将Jackson JSON对象引用转换为JSON。净对象引用

时间:2022-04-29 04:47:08

I need to be able to deserialize a JSON string produced by Jackson (Java/Spring server) with a C#/JSON.Net client while keeping the object references intact. Jackson uses "@id":1...n for cyclic references, while the reference is denoted by a single integer. JSON.Net uses "$id" and "$ref".

我需要能够用c# /JSON反序列化Jackson (Java/Spring服务器)生成的JSON字符串。Net客户端,同时保持对象引用的完整性。杰克逊使用“@ id”:1…循环引用为n,引用由一个整数表示。JSON。Net使用“$id”和“$ref”。

Does anybody know how to convert a JSON string from Jackson to a JSON.Net compatible version?

有人知道如何将JSON字符串从Jackson转换成JSON吗?网络兼容的版本吗?

1 个解决方案

#1


1  

Here is my solution:

这是我的解决方案:

Use these settings for JSON.Net:

使用这些设置为JSON.Net:

JsonSerializerSettings settings = new JsonSerializerSettings
{
    PreserveReferencesHandling = PreserveReferencesHandling.Objects,
};

Use this interceptor to convert the references:

使用此拦截器转换引用:

public static class JSONInterceptor
{
    public static string sanitizeJSON(string originalJSONFromJava)
    {
        // Get ID right from Jackson to JSON.Net
        string pattern = Regex.Escape(@"""@id"":") + "(\\d+)";
        string replacement = @"""$id"":""$1""";
        Regex rgx = new Regex(pattern);
        string output = rgx.Replace(originalJSONFromJava, replacement);

        // Convert Jackson reference in array
        pattern = @",(\d+)";
        replacement = @",{""$ref"":""$1""}";
        rgx = new Regex(pattern);
        output = rgx.Replace(output, replacement);

        // Convert single Jackson reference to ref
        pattern = Regex.Escape(@"""\\w+"":") + "(\\d+)";
        replacement = @"""$ref"":""$1""";
        rgx = new Regex(pattern);
        output = rgx.Replace(output, replacement);

        return output;
    }
}

Call the interceptor and deserialize like this:

像这样调用拦截器并反序列化:

asset = JsonConvert.DeserializeObject<Asset>(JSONInterceptor.sanitizeJSON(response), settings);

The asset class has this layout:

资产类别有如下布局:

public class Asset {

    ....

     // Parent asset
     public Asset parent;
     // Asset agents
     public List<Agents> agent;

     ....
}

So, Jackson produces something like:

所以,杰克逊产生了这样的东西:

{"@id":1,......."parent":{"@id":15,.....},"agents":[{"@id":6, ......},12,{...}]...}

which needs to be converted into something like (JSON.Net):

需要转换成(JSON.Net):

{"$id":"1",...,"$ref":"15",....,"agents":[{...,"$ref":"6",...]}

This is what the code above does.

上面的代码就是这样做的。

Hope this helps somebody.

希望这可以帮助别人。

#1


1  

Here is my solution:

这是我的解决方案:

Use these settings for JSON.Net:

使用这些设置为JSON.Net:

JsonSerializerSettings settings = new JsonSerializerSettings
{
    PreserveReferencesHandling = PreserveReferencesHandling.Objects,
};

Use this interceptor to convert the references:

使用此拦截器转换引用:

public static class JSONInterceptor
{
    public static string sanitizeJSON(string originalJSONFromJava)
    {
        // Get ID right from Jackson to JSON.Net
        string pattern = Regex.Escape(@"""@id"":") + "(\\d+)";
        string replacement = @"""$id"":""$1""";
        Regex rgx = new Regex(pattern);
        string output = rgx.Replace(originalJSONFromJava, replacement);

        // Convert Jackson reference in array
        pattern = @",(\d+)";
        replacement = @",{""$ref"":""$1""}";
        rgx = new Regex(pattern);
        output = rgx.Replace(output, replacement);

        // Convert single Jackson reference to ref
        pattern = Regex.Escape(@"""\\w+"":") + "(\\d+)";
        replacement = @"""$ref"":""$1""";
        rgx = new Regex(pattern);
        output = rgx.Replace(output, replacement);

        return output;
    }
}

Call the interceptor and deserialize like this:

像这样调用拦截器并反序列化:

asset = JsonConvert.DeserializeObject<Asset>(JSONInterceptor.sanitizeJSON(response), settings);

The asset class has this layout:

资产类别有如下布局:

public class Asset {

    ....

     // Parent asset
     public Asset parent;
     // Asset agents
     public List<Agents> agent;

     ....
}

So, Jackson produces something like:

所以,杰克逊产生了这样的东西:

{"@id":1,......."parent":{"@id":15,.....},"agents":[{"@id":6, ......},12,{...}]...}

which needs to be converted into something like (JSON.Net):

需要转换成(JSON.Net):

{"$id":"1",...,"$ref":"15",....,"agents":[{...,"$ref":"6",...]}

This is what the code above does.

上面的代码就是这样做的。

Hope this helps somebody.

希望这可以帮助别人。