Serialize composite object into a single json

Asked

Viewed 512 times

3

With Newtonsoft.Json, to the next object:

  public class Person
  {    
    public string Name { get; set; }
    public string GeneralText { get; set; }

    public Address Address { get; set; }
  }

  public class Address
  {
    public string Street { get; set; }
    public string Neighborhood { get; set; }
  }

I get the following json:

{
  "Name": "TEST",
  "GeneralText": "TEST",
  "Address": {
    "Street": "TEST",
    "Neighborhood": "TEST"
  }
}

However, I need, with this same object structure, a json in the following format:

{
  "Name": "TEST",
  "GeneralText": "TEST",
  "Street": "TEST",
  "Neighborhood": "TEST"
}

I mean, I need the class Adress not grouped, but only that its properties appear. How can I do this?

Edit 1: I’d like to create a generic class, like:

public class DefaultApiReturn<T>
{
    public T RetObject { get; set; }
}

All my API returns would have this class with the type of my return, but it would not be grouped by it in the return of Json, it would be to focus some things to be executed on all serializations, or other things. I could make her inherit from some class that configures things from my API without affecting the modeling of my object.

So instead of staying:

{
  "RetObject ": {
    "Property1": "TEST",
    "Property2": "TEST"
  }
}

I’d like you to stay

{
    "Property1": "TEST",
    "Property2": "TEST"
}
  • The return of the type T would still reflect the structure of the original object with its hierarchy... but with the preface Retobject, its Json firacaria { "RetObject": {...}}

  • The "Person" class I used was another example of what I’d like you to do. The hierarchy and composition of the T object can maintain, I would just like to "hide" the generic class. That by serializing this class, it serialized only its properties, not grouping them together.

  • Yeah, I wanted to remove this "Retobject", leave the properties in the root of json.

  • To leave everything on the "same level" has no magic, you will need to create a new object for this. You can do it manually for each type of return or use Reflection that will only increase complexity and impair performance. You have not yet informed the reason for this requirement, what is the problem of keeping the object structure in json?

  • The company I work for demands that a local framework be used for Apis. However, this framework asks the API classes to inherit from a certain class of them and this disturbs my modeling of objects. Their serialization is done with newtonsoft and if I could with some property of it solve this problem, I would create a generic class like I showed and always return it, and keep my modeling independent of the framework.

  • 1

    @Salatiel, from what I understand you would like to streamline the use framework of the company but from what I understand generics are serialized as object. What can you do to deserialize any object inside a type dynamic however, from my experience in companies, the control staff does not let code pass with data typed as Dynamic. I don’t know if this is your case, but companies invest a lot of capital to fit into typification frameworks, I think it’s ISO 42010 (I’m not sure), and if you show up with a dynamic typification model the guys veto without giving explanation.

  • Actually, using Dynamic would not be the best. However, I do have a lot of contact with the framework devs, and I’m trying to get them to change that by reducing the coupling. But it is still not guaranteed that this will happen and I already wanted to have a way to solve the coupling that this has created in my codes.

  • 1

    I see no need to change the framework... these templates are the service contracts, you must respect them on your side... If the service exists a certain model, you must change its structure with to meet these requirements. If you do not want to change your data layer and entities, use Dtos, Viewmodels and etc to serve them.

  • An alternative I thought during lunch was within your layer working with an XML data model and before transmitting the information process it with an XSLT generating the format requested by the framework.

  • Maybe.. I’ll give it one more try here to see if I can get something.

Show 5 more comments

1 answer

3

Simply assemble a new object with the desired structure for serialization.

var person = new Person
{
    Name = "Leandro",
    GeneralText = "teste",
    Address = new Address
    {
        Street = "Visconde de Nácar",
        Neighborhood = "Centro"
    }
};

var jsonResult = JsonConvert.SerializeObject(new
{
    person.Name,
    person.GeneralText,
    person.Address.Street,
    person.Address.Neighborhood
});

Upshot:

{
  "Name":"Leandro",
  "GeneralText":"teste",
  "Street":"Visconde de Nácar",
  "Neighborhood":"Centro"
}
  • But that would have to be specific to each one. I’m going to put an Edit there to better explain what I need.

  • 2

    You can even use Reflection for this, but it’s an unnecessary load and you’ll still have to be careful with repeated properties... My question is why do you need this planned object?

  • I put an Dit there trying to explain better why.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.