修改现有对象使用新的部分JSON数据Json.NET [英] Modify existing object with new partial JSON data using Json.NET
问题描述
考虑下面的示例程序
VAR日历=新的日历
{
n = 42,
CoffeeProvider =Espresso2000,
会议=新[]
{
新会议
{
位置=室1,
从= DateTimeOffset.Parse(2014-01-01T00:00:00Z),
要= DateTimeOffset.Parse(2014-01-01T01:00:00Z)
},
新的会议
{
位置=室2,
从= DateTimeOffset.Parse(2014-01-01T02:00:00Z),
要= DateTimeOffset.Parse( 2014-01-01T03:00:00Z)
},
}
};
VAR补丁= @{
'coffeeprovider':空,
'会议':[
{
'位置':'ROOM3',
'从':'2014-01-01T04:00:00Z',
',以':'2014-01-01T05:00:00Z'
}
]
};
VAR patchedCalendar =修补程序(日历,补丁);
的
修补程序()
方法的结果应该等于日历
除非补丁
的改变。这意味着, 编号
将保持不变, CoffeeProvider
将被设置为空
和会议
将包含位于ROOM3一个项目。
-
如何做一件创建一个一般
修补程序()
的方法,会为工作
中所述的对象(而不仅仅是例如日历对象)通过
Json.NET deserializable? -
如果(1)这是不可行的,这将是一些限制,将使可行以及如何将它实现?
您想要的 JsonSerializer.Populate()
:
< BLOCKQUOTE>
填充JSON值到目标对象。
块引用>
例如,这里正在更新你的日历实例
类:
公共静态类TestPopulate
{
公共静态无效测试()
{
VAR日历=新的日历
{
n = 42,
CoffeeProvider =Espresso2000,
会议=新[]
{
新会议
{
位置=室1,
从= DateTimeOffset.Parse(2014-01-01T00:00:00Z),
要= DateTimeOffset.Parse(2014年-01-01T01:00:00Z)
},
新会议
{
位置=室2,
从= DateTimeOffset.Parse(2014- 01-01T02:00:00Z),
要= DateTimeOffset.Parse(2014-01-01T03:00:00Z)
},
}
};
VAR补丁= @{
'coffeeprovider':空,
'会议':[
{
'位置':'ROOM3',
'从':'2014-01-01T04:00:00Z',
',以':'2014-01-01T05:00:00Z'
}
]
};
修补程序(日历,补丁);
的Debug.WriteLine(JsonConvert.SerializeObject(日历,Formatting.Indented));
}
公共静态无效补丁< T>(T OBJ,串补丁)
{
无功序列化=新JsonSerializer();使用(VAR读卡器=新StringReader(补丁))
{
serializer.Populate
(读卡器,OBJ);
}
}
}
和生产调试输出
{
标识:42,
coffeeprovider:空,
会议:
{
位置:ROOM3,
,由:2014-01-01T04:00:00 + 00:00,
要:2014-01-01T05:00:00 + 00:00
}
]
}
更新
如果你想先复制,你可以这样做:
公共静态牛逼CopyPatch< T>(T OBJ,串补丁)
{
无功序列化=新JsonSerializer( );
VAR JSON = JsonConvert.SerializeObject(OBJ);
VAR副本= JsonConvert.DeserializeObject< T>(JSON);使用(VAR读卡器=新StringReader(补丁))
{
serializer.Populate(读卡器,复制)
;
}
返回副本;
}
Consider the below example program
var calendar = new Calendar
{
Id = 42,
CoffeeProvider = "Espresso2000",
Meetings = new[]
{
new Meeting
{
Location = "Room1",
From = DateTimeOffset.Parse("2014-01-01T00:00:00Z"),
To = DateTimeOffset.Parse("2014-01-01T01:00:00Z")
},
new Meeting
{
Location = "Room2",
From = DateTimeOffset.Parse("2014-01-01T02:00:00Z"),
To = DateTimeOffset.Parse("2014-01-01T03:00:00Z")
},
}
};
var patch = @"{
'coffeeprovider': null,
'meetings': [
{
'location': 'Room3',
'from': '2014-01-01T04:00:00Z',
'to': '2014-01-01T05:00:00Z'
}
]
}";
var patchedCalendar = Patch(calendar, patch);
The result of the Patch()
method should be equal to calendar
except as changed by patch
. That means; Id
would be unchanged, CoffeeProvider
would be set to null
and Meetings
would contain a single item located in Room3.
How does one create a general
Patch()
method that will work for any object (not just the example Calendar object) deserializable by Json.NET?If (1) this is not feasible, what would be some restrictions that would make it feasible and how would it be implemented?
You want JsonSerializer.Populate()
:
Populates the JSON values onto the target object.
For instance, here it is updating an instance of your Calendar
class:
public static class TestPopulate
{
public static void Test()
{
var calendar = new Calendar
{
Id = 42,
CoffeeProvider = "Espresso2000",
Meetings = new[]
{
new Meeting
{
Location = "Room1",
From = DateTimeOffset.Parse("2014-01-01T00:00:00Z"),
To = DateTimeOffset.Parse("2014-01-01T01:00:00Z")
},
new Meeting
{
Location = "Room2",
From = DateTimeOffset.Parse("2014-01-01T02:00:00Z"),
To = DateTimeOffset.Parse("2014-01-01T03:00:00Z")
},
}
};
var patch = @"{
'coffeeprovider': null,
'meetings': [
{
'location': 'Room3',
'from': '2014-01-01T04:00:00Z',
'to': '2014-01-01T05:00:00Z'
}
]
}";
Patch(calendar, patch);
Debug.WriteLine(JsonConvert.SerializeObject(calendar, Formatting.Indented));
}
public static void Patch<T>(T obj, string patch)
{
var serializer = new JsonSerializer();
using (var reader = new StringReader(patch))
{
serializer.Populate(reader, obj);
}
}
}
And the debug output produced is:
{
"id": 42,
"coffeeprovider": null,
"meetings": [
{
"location": "Room3",
"from": "2014-01-01T04:00:00+00:00",
"to": "2014-01-01T05:00:00+00:00"
}
]
}
Update
If you want to copy first, you could do:
public static T CopyPatch<T>(T obj, string patch)
{
var serializer = new JsonSerializer();
var json = JsonConvert.SerializeObject(obj);
var copy = JsonConvert.DeserializeObject<T>(json);
using (var reader = new StringReader(patch))
{
serializer.Populate(reader, copy);
}
return copy;
}
这篇关于修改现有对象使用新的部分JSON数据Json.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!