0

Is there an existing way with Json.net to deserialize a json string into an anonymous object (which has the json properties only) using a concrete type as template ?

Exemple

JSON:

{ X: 1, Z: 2 }

Model:

public class Point {
    public float X { get; set; }
    public float Y { get; set; }
    public float Z { get; set; }
}

Desired deserialized ouput anonymous object :

new { X=1.0f, Z=1.0f } //Please note that X & Z are typed according to the Point class.

I want to do something like :

var @object = serializer.DeserializeAsAnonymous<Point>(json);

Why? I want to detect which properties of the model to update using reflection on both sides (model and provided deserialized json).

3 Answers 3

3
var point = new { X = 0.0f, Z = 0.0f };
var jsonResponse = 
    JsonConvert.DeserializeAnonymousType(json, point);
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for tour answer, but this is not what I am looking for. I want to use an existing type as template and deserialize the json provided properties into an anonymous object. Please, do not vote up.
@fso the answer does exactly what you are asking. Can you please clarify how this does not answer your question and we can see if we can address that.
the answer does exactly what you are asking --> NO 1) I do not want to instanciate an anonymous type before deserialization. 2) The template type used for deserialization should be static and should be used as a mask and for the properties type validation.
@fso You only need to instantiate the anonymous type so it can be used as a template; anonymous types have no class so it needs this to be used as a type. There is no other way to do it.
1

No, there is no way to do this unless you use code generation. Anonymous types are defined at compile time, whereas you are saying you don't know what properties you want in the anonymous type until you get the JSON (at run time).

If you're just trying to detect which properties were present in the JSON or not, you could deserialize into a JObject and then process that to figure out what changed. Alternatively, you could add a status dictionary to your model and then deserialize to it using a JsonConverter. See Is there a way in Json.NET serialization to distinguish between "null because not present" and "null because null"? for examples of these ideas.

1 Comment

Thanks, I just wanted to know if I need to write my own converter with or without some expression magic, this is not part of the Json.net library so I will implement it myself. PS: ty for the link.
1

serializer.Populate is the method I was looking for... It doesn't produce an anonymous object, but it populates an existing object with the existing json properties.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.