1

There are a number of great ways to auto-generate C# code from JSON, such as here and here.

However, the resulting code doesn't include property initializers. For example, the following JSON:

{
"Name" : "Blastoise"
}

gets deserialized to this:

public class RootObject
{
    public string Name { get; set; }
}

Presumably this is by design, since the values used in the JSON will probably be overridden anyways, so adding initializers might just annoy people who don't want them.

But what if I want that? Short of manually adding every value by hand, is there a way to deserialize JSON to the following?

public class RootObject
{
    public string Name { get; set; } = "Blastoise";
}

Obviously in this case a manual edit is easy, but manual editing becomes tedious for larger JSON objects.

Community
  • 1
  • 1
Peter Richter
  • 755
  • 1
  • 6
  • 15
  • How would you expect such a tool to handle a case where I send you two objects, each with "Name" having a different value? The point of the POCO generators is that they merely create a generic, reusuable object structure. – David L Sep 23 '15 at 20:34
  • *Initialization* is the deserialization of the json string. – Eser Sep 23 '15 at 20:37
  • For reference, Visual Studio does this on it's own now. You can paste JSON and XML as classes. – DavidG Sep 23 '15 at 20:37
  • @DavidG, maybe you can elaborate more in an answer – Arturo Torres Sánchez Sep 23 '15 at 20:46
  • @ArturoTorresSánchez I don't mean with initialisers, just creating the classes. Edit menu -> Paste special -> Paste JSON as classes – DavidG Sep 23 '15 at 20:58
  • @DavidG, well, that's the answer, isn't it? – Arturo Torres Sánchez Sep 23 '15 at 21:01
  • @DavidL Exactly. That's why I included the "Presumably this is by design" paragraph. My use case is that I'm serializing a class to send in a REST request, and most of the properties of the class are unchanging (only a few of the properties will be changed), and so default values allow me to skip `set`ting the unchanging properties every time. Perhaps I'm using the wrong approach? – Peter Richter Sep 23 '15 at 21:01
  • @ArturoTorresSánchez: No, I already mentioned the Visual Studio Paste Special method (see the links in my question). However, that doesn't answer the question, since my question is about initialization, not conversion. – Peter Richter Sep 23 '15 at 21:03
  • @PeterRichter it's possible it's the wrong approach, although I think this is more a matter of "the tools haven't caught up to a use case where it makes sense for C# 6 features", specifically auto property initializers. Like I mentioned, I'm not sure how you would expect the tool to know which properties SHOULD be auto-initialized and which SHOULDN'T. – David L Sep 23 '15 at 21:03

1 Answers1

5

is there a way to deserialize JSON to the following?

Using the source code of the converter you mentioned.

A quick change at the line 204

sw.WriteLine(prefix + "public {0} {1} {{ get; set; }} = {2};", field.Type.GetTypeName(), field.MemberName, field.GetExamplesText());

gives me the result similar to what you described

internal class SampleResponse1
{

    [JsonProperty("Name")]
    public string Name { get; set; } = "Blastoise";
}
rnort
  • 107
  • 1
  • 4