处理Java和C# REST API之间的JSON互操作

huangapple go评论72阅读模式
英文:

Dealing with JSON interop between Java and C# REST APIs

问题

我目前正在处理两个系统,它们通过各自的RESTful JSON API进行互操作。其中一个是使用C#和JSON.NET,另一个是Java Spring Boot Starter(Jackson JSON)。我对这两个系统都有完全的控制权。

这两个系统都需要传输具有引用处理的JSON数据。虽然这两个JSON序列化框架都支持引用处理,但C# JSON.NET使用“$id”和“$ref”语法来表示引用,而Java的Jackson则使用更简单的只有“id”。

我对Java不太熟悉,所以我更容易接受和理解任何关于如何在C#方面双向处理JSON引用的解决方案。我如何使这两个系统能够进行JSON引用的互操作?

C# JSON.NET引用处理文档。

来自Java Jackson的示例JSON

请注意,可以标记哪个类属性由Jackson用作引用。在这种情况下,我使用“Id”变量,因为它在类型上始终是唯一的。

{
  "Resources": [
      {
          "Id": 0,
          "Name": "Resource 0"
      },
      {
          "Id": 1,
          "Name": "Resource 1"
      }
  ],
  "Tasks": [
      {
          "Id": 0,
          "Name": "Task 0",
          "Resource": 0
      },
      {
          "Id": 1,
          "Name": "Task 1",
          "Resource": 1
      },
      {
          "Id": 2,
          "Name": "Task 2",
          "Resource": 0
      },
      {
          "Id": 3,
          "Name": "Task 3",
          "Resource": 1
      },
      {
          "Id": 4,
          "Name": "Task 4",
          "Resource": 0
      }
  ]
}
英文:

I'm currently dealing with 2 systems that expose interop via their own RESTful JSON APIs. One is in C# with JSON.NET and one is Java Spring Boot Starter (Jackson JSON). I have full control over both systems.

Both systems need to transfer JSON data with reference handling. Whilst both JSON serialization frameworks support it, C# JSON.NET uses "$id" and "$ref" syntax to signify references whilst Java's Jackson uses something plainer with only "id".

I am much less familiar with Java than I am C# so I would more readily accept and understand any solution on getting JSON ref handling working both ways on the C# side. How can I get these two systems to interop with JSON refs?

C# JSON.NET reference handling documentation.

Example JSON coming from Java Jackson

Note that it is possible to mark up what class property Jackson uses as the reference. In this case I am using the Id variable as it will always locally unique to the type.

{
  "Resources": [
      {
          "Id": 0,
          "Name": "Resource 0"
      },
      {
          "Id": 1,
          "Name": "Resource 1"
      }
  ],
  "Tasks": [
      {
          "Id": 0,
          "Name": "Task 0",
          "Resource": 0
      },
      {
          "Id": 1,
          "Name": "Task 1",
          "Resource": 1
      },
      {
          "Id": 2,
          "Name": "Task 2",
          "Resource": 0
      },
      {
          "Id": 3,
          "Name": "Task 3",
          "Resource": 1
      },
      {
          "Id": 4,
          "Name": "Task 4",
          "Resource": 0
      }
  ]
}

答案1

得分: 4

Before reading, check out my other solution approach here, it may be simpler.
Keeping this post as well as I believe it is informative and may be considered a better approach by some.


Why is this complicated?

The issue is not the reference property names, for that you can use IReferenceResolver to override. Instead, the issue is two-fold:

  1. The reference is from a property of an object in the Tasks list to an object in the Resources list. This is not the intention of the PreserveObjectReference feature. It was intended to not repeat objects in the same list as well as help prevent cyclic references.

  2. The value in the Resource property of a Task is a number instead of a Resource object (which would not have worked anyways, due to item 1 above), e.g.

{
  "Id": 0,
  "Name": "Task 0",
  "Resource": {
    "$ref": 0
  }
}
  1. IDs and references have to be strings, not numbers

Solution

Manually build the object and manually match the references:

  1. Our DTOs:
public class Dto
{
    public Resource[] Resources { get; set; }
    public Task[] Tasks { get; set; }
}

public class Resource
{
    public long Id { get; set; }
    public string Name { get; set; }
}

public class Task
{
    public long Id { get; set; }
    public string Name { get; set; }
    public Resource Resource { get; set; }
}
  1. Contract resolution:
/// <summary>
/// This is to resolve the Resource resolver for the Task
/// </summary>
internal class TaskResourceContractResolver : DefaultContractResolver
{
    private readonly IDictionary<long, Resource> _resources;

    public TaskResourceContractResolver(IDictionary<long, Resource> resources) => this._resources = resources;

    #region Overrides of DefaultContractResolver
    protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
    {
        var property = base.CreateProperty(member, memberSerialization);
        if (property.DeclaringType != typeof(Task) || property.PropertyName != nameof(Task.Resource))
            return property;

        property.Converter = new TaskResourceConverter(this._resources);
        property.IsReference = true;
        property.ValueProvider = new CurrentValueGetterValueProvider();
        return property;
    }
    #endregion Overrides of DefaultContractResolver

    /// <summary>
    /// This is to resolve the Resource for the Task
    /// </summary>
    private class TaskResourceConverter : JsonConverter<Task>
    {
        private readonly IDictionary<long, Resource> _resources;

        public TaskResourceConverter(IDictionary<long, Resource> resources) => this._resources = resources;

        #region Overrides of JsonConverter
        public override void WriteJson(JsonWriter writer, Task value, JsonSerializer serializer) => throw new NotImplementedException();
        public override Task ReadJson(JsonReader reader, Type objectType, Task existingValue, bool hasExistingValue, JsonSerializer serializer)
        {
            if (reader.Value is Resource resource) existingValue.Resource = resource;
            else if (reader.Value is long resourceRef)
            {
                if (!this._resources.TryGetValue(resourceRef, out resource)) throw new Exception($"Invalid resource reference '{resourceRef}'");
                existingValue.Resource = resource;
            }
            else throw new Exception($"Invalid resource reference '{reader.Value}'");

            return existingValue;
        }
        #endregion Overrides of JsonConverter
    }

    /// <summary>
    /// This is so we get the value of the Task object to be set
    /// </summary>
    private class CurrentValueGetterValueProvider : IValueProvider
    {
        #region Implementation of IValueProvider
        public void SetValue(object target, object value) => throw new NotImplementedException();
        public object GetValue(object target) => target;
        #endregion Implementation of IValueProvider
    }
}
  1. Implementation:
var input = Encoding.UTF8.GetString(Properties.Resources.input); // the posted Java-outputted JSON
var parsed = JObject.Parse(input);

var resources = parsed[nameof(Dto.Resources)]?.Children()
    .Select(token => token.ToObject<Resource>())
    .ToDictionary(r => r!.Id);

var serializer = new JsonSerializer() { ContractResolver = new TaskResourceContractResolver(resources) };

var dto = new Dto
{
    Resources = resources?.Values.ToArray(),
    Tasks = parsed[nameof(Dto.Tasks)]?.Children()
        .Select(token => token.ToObject<Task>(serializer))
        .ToArray()
};

Console.WriteLine($@"Distinct resources: {dto.Resources?.Distinct().Count()}");
Console.WriteLine($@"Distinct tasks: {dto.Tasks?.Distinct().Count()}");
Console.WriteLine($@"Distinct task resources: {dto.Tasks?.Select(t => t.Resource).Distinct().Count()}");
  1. Output:
Distinct resources: 2
Distinct tasks: 5
Distinct task resources: 2

.NET Fiddle

英文:

Before reading, check out my other solution approach here, it may be simpler.
Keeping this post as well as I believe it is informative and may be consider a better approach by some.


Why is this complicated?

The issue is not the reference property names, for that you can use IReferenceResolver to override. Instead, the issue is two-fold:

  1. The reference is from a property of an object in the Tasks list to an object in the Resources list. This is not the intention of the PreserveObjectReference feature. It was intended to not repeat objects in the same list as well as help prevent cyclic references.

  2. The value in the Resource property of a Task is a number instead of a Resource object (which would not have worked anyways, due to item 1 above), e.g.

{
  &quot;Id&quot;: 0,
  &quot;Name&quot;: &quot;Task 0&quot;,
  &quot;Resource&quot;: {
    &quot;$ref&quot;: 0
  }
}
  1. IDs and references have to be strings, not numbers

Solution

Manually build the object and manually match the references:

  1. Our DTOs:
public class Dto
{
    public Resource[] Resources { get; set; }
    public Task[] Tasks { get; set; }
}

public class Resource
{
    public long Id { get; set; }
    public string Name { get; set; }
}

public class Task
{
    public long Id { get; set; }
    public string Name { get; set; }
    public Resource Resource { get; set; }
}
  1. Contract resolution:
/// &lt;summary&gt;
/// This is to resolve the Resource resolver for the Task
/// &lt;/summary&gt;
internal class TaskResourceContractResolver : DefaultContractResolver
{
    private readonly IDictionary&lt;long, Resource&gt; _resources;

    public TaskResourceContractResolver(IDictionary&lt;long, Resource&gt; resources) =&gt; this._resources = resources;

    #region Overrides of DefaultContractResolver
    protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
    {
        var property = base.CreateProperty(member, memberSerialization);
        if (property.DeclaringType != typeof(Task) || property.PropertyName != nameof(Task.Resource))
            return property;

        property.Converter = new TaskResourceConverter(this._resources);
        property.IsReference = true;
        property.ValueProvider = new CurrentValueGetterValueProvider();
        return property;
    }
    #endregion Overrides of DefaultContractResolver

    /// &lt;summary&gt;
    /// This is to resolve the Resource for the Task
    /// &lt;/summary&gt;
    private class TaskResourceConverter : JsonConverter&lt;Task&gt;
    {
        private readonly IDictionary&lt;long, Resource&gt; _resources;

        public TaskResourceConverter(IDictionary&lt;long, Resource&gt; resources) =&gt; this._resources = resources;

        #region Overrides of JsonConverter
        public override void WriteJson(JsonWriter writer, Task value, JsonSerializer serializer) =&gt; throw new NotImplementedException();
        public override Task ReadJson(JsonReader reader, Type objectType, Task existingValue, bool hasExistingValue, JsonSerializer serializer)
        {
            if (reader.Value is Resource resource) existingValue.Resource = resource;
            else if (reader.Value is long resourceRef)
            {
                if (!this._resources.TryGetValue(resourceRef, out resource)) throw new Exception($&quot;Invalid resource reference &#39;{resourceRef}&#39;&quot;);
                existingValue.Resource = resource;
            }
            else throw new Exception($&quot;Invalid resource reference &#39;{reader.Value}&#39;&quot;);

            return existingValue;
        }
        #endregion Overrides of JsonConverter
    }

    /// &lt;summary&gt;
    /// This is so we get the value of Task object to be set
    /// &lt;/summary&gt;
    private class CurrentValueGetterValueProvider : IValueProvider
    {
        #region Implementation of IValueProvider
        public void SetValue(object target, object value) =&gt; throw new NotImplementedException();
        public object GetValue(object target) =&gt; target;
        #endregion Implementation of IValueProvider
    }
}
  1. Implementation:
var input = Encoding.UTF8.GetString(Properties.Resources.input); // the posted Java-outputted JSON
var parsed = JObject.Parse(input);

var resources = parsed[nameof(Dto.Resources)]?.Children()
    .Select(token =&gt; token.ToObject&lt;Resource&gt;())
    .ToDictionary(r =&gt; r!.Id);

var serializer = new JsonSerializer() { ContractResolver = new TaskResourceContractResolver(resources) };

var dto = new Dto
{
    Resources = resources?.Values.ToArray(),
    Tasks = parsed[nameof(Dto.Tasks)]?.Children()
        .Select(token =&gt; token.ToObject&lt;Task&gt;(serializer))
        .ToArray()
};

Console.WriteLine($@&quot;Distinct resources: {dto.Resources?.Distinct().Count()}&quot;);
Console.WriteLine($@&quot;Distinct tasks: {dto.Tasks?.Distinct().Count()}&quot;);
Console.WriteLine($@&quot;Distinct task resources: {dto.Tasks?.Select(t =&gt; t.Resource).Distinct().Count()}&quot;);
  1. Output:
Distinct resources: 2
Distinct tasks: 5
Distinct task resources: 2

.NET Fiddle

答案2

得分: 0

可能是我在这里发布的更简单的方法

原因是相同的,但解决方案不同:

使用一个中间的仅序列化类

(这个类既有序列化解决方案,也有反序列化解决方案)

  1. DTOs 是相同的:
public class Dto
{
    public Resource[] Resources { get; set; }
    public Task[] Tasks { get; set; }
}

public class Resource
{
    public long Id { get; set; }
    public string Name { get; set; }
}

public class Task
{
    public long Id { get; set; }
    public string Name { get; set; }
    public Resource Resource { get; set; }
}
  1. 中间的仅序列化类:
/// <summary>
/// Dto 序列化的辅助类
/// </summary>
internal class DtoSerializationHelper
{
    public Resource[] Resources { get; set; }

    /// <summary>
    /// 供应用程序代码使用(而不是用于)
    /// </summary>
    [JsonIgnore]
    public Task[] Tasks { get; set; }

    /// <summary>
    /// 由序列化器使用
    /// </summary>
    [JsonProperty(nameof(Tasks))]
    private TaskSerializationHelper[] SerializationTasks { get; set; }

    [OnDeserialized]
    private void OnDeserialized(StreamingContext context)
    {
        var resourceLookup = this.Resources.ToDictionary(r => r.Id);
        this.Tasks = this.SerializationTasks.Select(t => t.ToTask(resourceLookup)).ToArray();
    }

    [OnSerializing]
    private void OnSerializing(StreamingContext context)
    {
        this.SerializationTasks = this.Tasks?.Select(t => new TaskSerializationHelper(t)).ToArray();
    }

    /// <summary>
    /// 在强制转换时从辅助类转换为 Dto
    /// </summary>
    /// <param name="helper"></param>
    public static implicit operator Dto(DtoSerializationHelper helper) => new Dto
    {
        Resources = helper.Resources,
        Tasks = helper.Tasks
    };

    /// <summary>
    /// 在强制转换时从 Dto 转换为辅助类
    /// </summary>
    /// <param name="dto"></param>
    public static explicit operator DtoSerializationHelper(Dto dto) => new DtoSerializationHelper
    {
        Resources = dto.Resources,
        Tasks = dto.Tasks
    };

    /// <summary>
    /// 一个 Task 序列化辅助类
    /// </summary>
    private class TaskSerializationHelper
    {
        public TaskSerializationHelper() { }

        public TaskSerializationHelper(Task task) : this()
        {
            this.Id = task.Id;
            this.Name = task.Name;
            this.Resource = task.Resource.Id;
        }

        public long Id { get; set; }
        public string Name { get; set; }
        public long Resource { get; set; }

        public Task ToTask(IDictionary<long, Resource> resourceLookup) =>
            new Task
            {
                Id = this.Id,
                Name = this.Name,
                Resource = resourceLookup is null || !resourceLookup.TryGetValue(this.Resource, out var resource)
                    ? throw new Exception($"Invalid resource {this.Resource}")
                    : resource
            };
    }
}
  1. 实现:
var input = Encoding.UTF8.GetString(Properties.Resources.input); // 发布的 Java 输出的 JSON

var dtoSerializationHelper = JsonConvert.DeserializeObject<DtoSerializationHelper>(input);
var dto = (Dto)dtoSerializationHelper;

var deserializationResults = new
{
    distinctResources = dto.Resources?.Distinct().Count(),
    distinctTasks = dto.Tasks?.Distinct().Count(),
    distinctTaskResources = dto.Tasks?.Select(t => t.Resource).Distinct().Count()
};

Console.WriteLine($"Distinct resources: {deserializationResults.distinctResources}");
Console.WriteLine($"Distinct tasks: {deserializationResults.distinctTasks}");
Console.WriteLine($"Distinct task resources: {deserializationResults.distinctTaskResources}");

if (deserializationResults.distinctResources != 2 ||
    deserializationResults.distinctTasks != 5 ||
    deserializationResults.distinctTaskResources != 2) throw new Exception("Deserialization failed");

Console.WriteLine();

var output = JsonConvert.SerializeObject((DtoSerializationHelper)dto);
var serializationResult = output == input;

Console.WriteLine($"Input and output are same: {serializationResult}");
if (serializationResult) return;

Console.WriteLine($"Output: {output}");
throw new Exception("Serialization failed");
  1. 输出:
Distinct resources: 2
Distinct tasks: 5
Distinct task resources: 2
Input and output are same: True

.NET Fiddle

英文:

Possibly a simpler approach to what I published here

Reasons are the same, but solution is different:

Use an interim serialization-only class

(this one has both serialization and deserialization solution)

  1. DTOs are the same:
public class Dto
{
    public Resource[] Resources { get; set; }
    public Task[] Tasks { get; set; }
}

public class Resource
{
    public long Id { get; set; }
    public string Name { get; set; }
}

public class Task
{
    public long Id { get; set; }
    public string Name { get; set; }
    public Resource Resource { get; set; }
}
  1. Interim serialization-only class:
/// &lt;summary&gt;
/// Helper class for Dto serialization
/// &lt;/summary&gt;
internal class DtoSerializationHelper
{
    public Resource[] Resources { get; set; }

    /// &lt;summary&gt;
    /// To be used by application code (not for 
    /// &lt;/summary&gt;
    [JsonIgnore]
    public Task[] Tasks { get; set; }

    /// &lt;summary&gt;
    /// Used by serializer
    /// &lt;/summary&gt;
    [JsonProperty(nameof(Tasks))]
    private TaskSerializationHelper[] SerializationTasks { get; set; }

    [OnDeserialized]
    private void OnDeserialized(StreamingContext context)
    {
        var resourceLookup = this.Resources.ToDictionary(r =&gt; r.Id);
        this.Tasks = this.SerializationTasks.Select(t =&gt; t.ToTask(resourceLookup)).ToArray();
    }

    [OnSerializing]
    private void OnSerializing(StreamingContext context)
    {
        this.SerializationTasks = this.Tasks?.Select(t =&gt; new TaskSerializationHelper(t)).ToArray();
    }

    /// &lt;summary&gt;
    /// Converts from the helper to the Dto when casting
    /// &lt;/summary&gt;
    /// &lt;param name=&quot;helper&quot;&gt;&lt;/param&gt;
    public static implicit operator Dto(DtoSerializationHelper helper) =&gt; new Dto
    {
        Resources = helper.Resources,
        Tasks = helper.Tasks
    };

    /// &lt;summary&gt;
    /// Converts from the Dto to the helper when casting
    /// &lt;/summary&gt;
    /// &lt;param name=&quot;dto&quot;&gt;&lt;/param&gt;
    public static explicit operator DtoSerializationHelper(Dto dto) =&gt; new DtoSerializationHelper
    {
        Resources = dto.Resources,
        Tasks = dto.Tasks
    };

    /// &lt;summary&gt;
    /// A Task serialization helper class
    /// &lt;/summary&gt;
    private class TaskSerializationHelper
    {
        public TaskSerializationHelper() { }

        public TaskSerializationHelper(Task task) : this()
        {
            this.Id = task.Id;
            this.Name = task.Name;
            this.Resource = task.Resource.Id;
        }

        public long Id { get; set; }
        public string Name { get; set; }
        public long Resource { get; set; }

        public Task ToTask(IDictionary&lt;long, Resource&gt; resourceLookup) =&gt;
            new Task
            {
                Id = this.Id,
                Name = this.Name,
                Resource = resourceLookup is null || !resourceLookup.TryGetValue(this.Resource, out var resource)
                    ? throw new Exception($&quot;Invalid resource {this.Resource}&quot;)
                    : resource
            };
    }
}
  1. Implementation:
var input = Encoding.UTF8.GetString(Properties.Resources.input); // the posted Java-outputted JSON

var dtoSerializationHelper = JsonConvert.DeserializeObject&lt;DtoSerializationHelper&gt;(input);
var dto = (Dto)dtoSerializationHelper;

var deserializationResults = new
{
    distinctResources = dto.Resources?.Distinct().Count(),
    distinctTasks = dto.Tasks?.Distinct().Count(),
    distinctTaskResources = dto.Tasks?.Select(t =&gt; t.Resource).Distinct().Count()
};

Console.WriteLine($@&quot;Distinct resources: {deserializationResults.distinctResources}&quot;);
Console.WriteLine($@&quot;Distinct tasks: {deserializationResults.distinctTasks}&quot;);
Console.WriteLine($@&quot;Distinct task resources: {deserializationResults.distinctTaskResources}&quot;);

if (deserializationResults.distinctResources != 2 ||
    deserializationResults.distinctTasks != 5 ||
    deserializationResults.distinctTaskResources != 2) throw new Exception(&quot;Deserialization failed&quot;);

Console.WriteLine();

var output = JsonConvert.SerializeObject((DtoSerializationHelper)dto);
var serializationResult = output == input;

Console.WriteLine($@&quot;Input and output are same: {serializationResult}&quot;);
if (serializationResult) return;

Console.WriteLine($@&quot;Output: {output}&quot;);
throw new Exception(&quot;Serialization failed&quot;);
  1. Output:
Distinct resources: 2
Distinct tasks: 5
Distinct task resources: 2
Input and output are same: True

.NET Fiddle

huangapple
  • 本文由 发表于 2020年10月1日 13:13:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/64149445.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定