Is there a way to populate an existing object from json using json.net based interface

Looking for a way to do something like the following:

NewtonSoft.Json.JsonConvert.Populate<IMyContract>(jsonStr, currentObj); 

where the json.net engine will only attempt to populate the properties identified in IMyContract.

Any suggestions?

+4
source share
1 answer

Perhaps you can use

 PopulateObject(String, Object, JsonSerializerSettings) 

And use your own JsonSerializerSettings settings. See: http://james.newtonking.com/projects/json/help/html/T_Newtonsoft_Json_JsonSerializerSettings.htm

Try using one of the delegates to limit the properties that are set themselves. It can be based on the interface using reflection.

I do not know another way. If you do not get it using JsonSerializerSettings, you can always write your own PopulateObject method using the Json framework.

+2
source

Source: https://habr.com/ru/post/1392564/


All Articles