1

For example I have classes:

public class ParentClass
{
  public int Value1 {get; set;} = 1;
  public int Value2 {get; set;} = 2;
  public InnerClass InnClass {get; set;} = new InnerClass();
}

public class InnerClass
{
  public int Value3 {get; set;} = 3;
  public int Value4 {get; set;} = 4;
}

When I serialize an instance of the ParentClass

public ParentClass parClass = new ParentClass();
string result = JsonConvert.SerializeObject(parClass);

it returns Json:

“{“Value1”: 1, “Value2”: 2, “InnClass”: {“Value3”: 3, “Value4”: 4}}”

How to make it serialize like this:

“{“Value1”: 1, “Value2”: 2, “Value3”: 3, “Value4”: 4}”

I know that the easiest way is just to move properties from inner class to the parent class, and then to serialize it. But it will break the structure of the program. So is it any way to do this with a custom JsonConverter or other way?

Alex Vince
  • 11
  • 2
  • Where are you serializing this at, your post isn't including everything? The way it's serializing now is indeed correct, why would you not want to do this? When you go to deserialize your object, everything would get deserialized correctly. – Trevor Apr 16 '21 at 15:20
  • It includes everything. I just use JsonConvert.SerializeObject() method – Alex Vince Apr 16 '21 at 15:29
  • Why do you want to change this, how would you deserialize it if you don't know where the properties belong? – Trevor Apr 16 '21 at 15:31
  • There is no task to deserialize it. – Alex Vince Apr 16 '21 at 15:35
  • `JsonConverter` what is this, maybe it should be `JsonConvert`? – Trevor Apr 16 '21 at 15:40
  • Yes, JsonConvert.SerializeObject() – Alex Vince Apr 16 '21 at 15:46
  • `ParentClass parentClass = new ParentClass(); string result = JsonConvert.SerializeObject(new { parentClass.Value1, parentClass.Value2, parentClass.InnClass.Value3, parentClass.InnClass.Value4 }, Formatting.Indented);` == `{ "Value1": 1, "Value2": 2, "Value3": 3, "Value4": 4 }` you don't need anything special to do this. – Trevor Apr 16 '21 at 16:05
  • Yes, it’s similar to solution with dynamic object. I know it is much easier to use anonymous type or some intermediate class to move all properties in it and then serialize it. But what if you don’t have a permission to create any new objects or anonymous types or modify the Json string by yourself. And the server can accept the input json only in specified format. So the only way you can do something is to find work solution with custom JsonConverter or rewrite all the project. – Alex Vince Apr 16 '21 at 16:26
  • There's no attribute for that. You could use a [custom JsonConverter](https://www.newtonsoft.com/json/help/html/CustomJsonConverter.htm) like the one from [Deserialize JSON object into nested C# object](https://stackoverflow.com/q/51029873/3744182). Or you could serialize an intermediate DTO such as an anonymous type object with the required properties at the top level. – dbc Apr 16 '21 at 16:30
  • In fact this looks like a duplicate of [Deserialize JSON object into nested C# object](https://stackoverflow.com/q/51029873/3744182), agree? – dbc Apr 16 '21 at 18:15
  • It looks similar, but in that example is the contrary task. How to make a model with a nested class from the plain Json. My task is the opposite - how to make a plain json from a model with nested objects. And it looks like with out creating an intermediate object it is impossible. Because it is impossible to get a parent object from inner object serializer. – Alex Vince Apr 17 '21 at 05:07
  • @AlexVince - But the answer is the same. The serializer works in both directions, and the converter shown there both serializes and deserializes. – dbc Apr 17 '21 at 16:39

1 Answers1

0

Create a dynamic object, put values in it, and serialize it to JSON.

J S
  • 591
  • 6
  • 11
  • Yes it is the easiest way just to create a dynamic object or to move properties in 1 class together. But I still want to know if it is possible to do it with JsonWriter or ContractReslover – Alex Vince Apr 16 '21 at 15:21