1

I want to serialize/deserialize following classes into/from JSON:

public class Employee 
{
    string name;
    Position position;
}

public class Position 
{
    string positionName;
    int salary;
}

The tricky part is that I want to treat Position fields as Employee fields, so JSON would look like this:

{
    "name": "John",
    "positionName": "Manager",
    "salary" : 1000
}

How to achieve this using Json.NET ?

Kao
  • 7,225
  • 9
  • 41
  • 65

4 Answers4

2

You have either to deserialize it as anonymous object either (recommended ) implement a custom deserialization as stated here:

Merge two objects during serialization using json.net?

Please let us know if there any more questions.

Here's an example (you can find it in the provided link):

public class FlattenJsonConverter : JsonConverter
{
    public override void WriteJson(JsonWriter writer, object value, 
        JsonSerializer serializer)
    {
        JToken t = JToken.FromObject(value);
        if (t.Type != JTokenType.Object)
        {
            t.WriteTo(writer);
            return;
        }

        JObject o = (JObject)t;
        writer.WriteStartObject();
        WriteJson(writer, o);
        writer.WriteEndObject();
    }

    private void WriteJson(JsonWriter writer, JObject value)
    {
        foreach (var p in value.Properties())
        {
            if (p.Value is JObject)
                WriteJson(writer, (JObject)p.Value);
            else
                p.WriteTo(writer);
        }
    }

    public override object ReadJson(JsonReader reader, Type objectType, 
       object existingValue, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }

    public override bool CanConvert(Type objectType)
    {
        return true; // works for any type
    }
}
Community
  • 1
  • 1
Felix Av
  • 1,254
  • 1
  • 14
  • 22
2

My solution is this

static void Main(string[] args)
        {
            Position p = new Position();
            p.positionName = "Manager";
            p.salary = 1000;

            Employee e = new Employee();
            e.name = "John";
            e.position = p;

            ResultJson r = new ResultJson();
            r.name = e.name;
            r.positionName = e.position.positionName;
            r.salary = e.position.salary;

            var result = JsonConvert.SerializeObject(r);

            Console.WriteLine(result);
            Console.ReadLine();
        }

    }

    public class Employee
    {
        public string name { get; set; }
        public Position position { get; set; }
    }

    public class Position
    {
        public string positionName { get; set; }
        public int salary { get; set; }
    }

    public class ResultJson
    {
        public string name { get; set; }
        public string positionName { get; set; }
        public int salary { get; set; }
    }

use seperate model for result

Mostafiz
  • 7,243
  • 3
  • 28
  • 42
  • wouldn't recommend it as this solution is very specific and require a model class for every type of result that will be needed – Felix Av Apr 12 '16 at 14:06
-1

You can use this code with NewtonSoft.Json library

[JsonObject]
public class Employee 
{
    [JsonProperty("name")]
    string name;
    [JsonProperty("positionName")]
    string positionName;
    [JsonProperty("salary")]
    int salary;
}

Use One class instead 2, or realize own parser

SYL
  • 337
  • 4
  • 16
-2

try in this way. { "name": "John", "position": [{ "positionName": "Manager", "salary" : 1000 }] }

divya
  • 196
  • 11
  • This won't work. Moreover, I want to both serialize and deserialize. – Kao Apr 12 '16 at 10:53
  • try removing "[ ]"(square bracket ). for more you can refer https://msdn.microsoft.com/en-us/library/bb412179(v=vs.110).aspx – divya Apr 12 '16 at 10:57