簡體   English   中英

是否有JSON.Net的JavaScript序列化器?

[英]Is there a javascript serializer for JSON.Net?

我正在使用Newtonsoft JSON.Net對啟用了PreserveReferencesHandling的對象進行反序列化。 jQuery不支持基於JSON.Net使用的$ ref和$ id語法重新鏈接引用(我不知道jQuery是否以任何能力支持此功能)。

我嘗試使用道格拉斯· 克羅克福德( Douglas Crockford)的cycle.js,但這似乎不適用於我的對象,返回的對象與傳入的對象相同。

我對JSON.Net並不是很熟悉,但是我似乎找不到任何可以序列化(或解析)其.NET組件輸出的JSON的JavaScript庫。

如何完成將對象引用放回原處?

我也在尋找解決這個問題的方法,最終入侵了Douglas Crockford的JSON.retrocycle函數。 他的函數不適用於$ ref = 某個數字 ,但它看起來像xpath。

這是我的又快又臟的版本-不要按原樣使用它-我沒有做任何清理,它可能應該是一個插件,但是它確實可以完成工作,並且足夠進行:

function retrocycle(o) {
var self = this;
self.identifiers = [];
self.refs = [];

self.rez = function (value) {

    // The rez function walks recursively through the object looking for $ref
    // properties. When it finds one that has a value that is a path, then it
    // replaces the $ref object with a reference to the value that is found by
    // the path.

    var i, item, name, path;

    if (value && typeof value === 'object') {
        if (Object.prototype.toString.apply(value) === '[object Array]') {
            for (i = 0; i < value.length; i += 1) {
                item = value[i];
                if (item && typeof item === 'object') {
                    path = item.$ref;
                    if (typeof path === 'string' && path != null) {
                        //self.refs[parseInt(path)] = {};

                        value[i] = self.identifiers[parseInt(path)]
                    } else {
                        self.identifiers[parseInt(item.$id)] = item;
                        self.rez(item);
                    }
                }
            }
        } else {
            for (name in value) {
                if (typeof value[name] === 'object') {
                    item = value[name];
                    if (item) {
                        path = item.$ref;
                        if (typeof path === 'string' && path != null) {
                            //self.refs[parseInt(path)] = {};

                            value[name] = self.identifiers[parseInt(path)]
                        } else {
                            self.identifiers[parseInt(item.$id)] = item;
                            self.rez(item);
                        }
                    }
                }
            }
        }
    }

};
self.rez(o);
self.identifiers = [];
}

像這樣使用它:

    $.post("url/function", { ID: params.ID }, function (data) {

        retrocycle(data)

        // data references should be fixed up now

    }, "json");

您將不得不對js解析器進行雙重查找。 從技術上講,保留引用處理是為了避免循環引用,這就是在解析期間通常會導致堆棧溢出的原因。

JSON沒有用於處理此問題的本機語法。 Newtonsoft版本是一個自定義實現,因此解析JSON將是一個自定義實現。

如果確實必須保留此類引用,那么XML可能是一個更好的解決方案。 有一些json-> xml庫。

這是一個可能有用的解析解決方案,或者至少是一個指南: https : //blogs.oracle.com/sundararajan/entry/a_convention_for_circular_reference

這是@Dimitri的增強版本。 @Dimitri代碼有時無法重建引用。 如果有人改進了代碼,請告訴我。

此致,Marco Alves。

if (typeof JSON.retrocycle !== 'function') {
    JSON.retrocycle = function retrocycle(o) {
        //debugger;

        var self = this;
        self.identifiers = [];
        self.refs = [];

        self.buildIdentifiers = function (value) {
            //debugger;

            if (!value || typeof value !== 'object') {
                return;
            }

            var item;

            if (Object.prototype.toString.apply(value) === '[object Array]') {
                for (var i = 0; i < value.length; i += 1) {
                    item = value[i];

                    if (!item || !item.$id || isNaN(item.$id)) {
                        if (item) {
                            self.buildIdentifiers(item);
                        }

                        continue;
                    }

                    self.identifiers[parseInt(item.$id)] = item;
                    self.buildIdentifiers(item);
                }

                return;
            }

            for (var name in value) {
                if (typeof value[name] !== 'object') {
                    continue;
                }

                item = value[name];

                if (!item || !item.$id || isNaN(item.$id)) {
                    if (item) {
                        self.buildIdentifiers(item);
                    }

                    continue;
                }

                self.identifiers[parseInt(item.$id)] = item;
                self.buildIdentifiers(item);
            }
        };

        self.rez = function (value) {

            // The rez function walks recursively through the object looking for $ref
            // properties. When it finds one that has a value that is a path, then it
            // replaces the $ref object with a reference to the value that is found by
            // the path.

            var i, item, name, path;

            if (value && typeof value === 'object') {
                if (Object.prototype.toString.apply(value) === '[object Array]') {
                    for (i = 0; i < value.length; i += 1) {
                        item = value[i];
                        if (item && typeof item === 'object') {

                            if (item.$ref)
                                path = item.$ref;

                            if (typeof path === 'string' && path != null) {
                                //self.refs[parseInt(path)] = {};

                                value[i] = self.identifiers[parseInt(path)];
                                continue;
                            }

                            //self.identifiers[parseInt(item.$id)] = item;
                            self.rez(item);
                        }
                    }
                } else {
                    for (name in value) {
                        if (typeof value[name] === 'object') {
                            item = value[name];
                            if (item) {
                                path = item.$ref;
                                if (typeof path === 'string' && path != null) {
                                    //self.refs[parseInt(path)] = {};

                                    value[name] = self.identifiers[parseInt(path)];
                                    continue;
                                }

                                //self.identifiers[parseInt(item.$id)] = item;
                                self.rez(item);
                            }
                        }
                    }
                }
            }

        };

        self.buildIdentifiers(o);
        self.rez(o);
        self.identifiers = []; // Clears the array
    };
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM