1

I want to serialize properties but with each property enclosed in a separate JSON object in an array.

Example below:

public class Metadata
{
    public string Name { get; set; }
    public Guid Id { get; set; }
}

public void SerializeCars()
{
    var data = new Metadata { Name = "MyName", Id = Guid.NewGuid() };
    var json = JsonConvert.SerializeObject(data, Formatting.Indented);
}

Current result will be:

{
    "Name": "MyName",
    "Id": "f9c4bc06-0b99-47ff-b22b-ea094fc188ee"
}

I want it to be (missing "td" class above):

{
    "td": [{
        "name": "myName"
    }, {
        "id": "f9c4bc06-0b99-47ff-b22b-ea094fc188ee"
    }]
}

Can it be fixed?

Brian Rogers
  • 125,747
  • 31
  • 299
  • 300
DNPE
  • 63
  • 7
  • 7
    Your "I want it to be" JSON is not valid JSON. – user5090812 Mar 24 '16 at 13:59
  • Why do you wan't it to be like this? – Mafii Mar 24 '16 at 14:01
  • Above json is wrapped in an array. Missed that in example above. So target json is like "md": [ {"name": "myName"}, {"id": "f9c4bc06-0b99-47ff-b22b-ea094fc188ee" } ] – DNPE Mar 24 '16 at 14:08
  • It is not typical to have an object of unnamed objects. An array of objects is the correct representation, but I'll see if I can whip up a way to make it work for you. – james Mar 24 '16 at 14:13

2 Answers2

1

You can create a custom JsonConverter to get the output you want:

class MetadataConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return (objectType == typeof(Metadata));
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        JArray array = new JArray();
        foreach (PropertyInfo pi in value.GetType().GetProperties().Where(p => p.CanRead))
        {
            array.Add(new JObject(new JProperty(pi.Name, pi.GetValue(value, null))));
        }
        JObject obj = new JObject(new JProperty("td", array));
        obj.WriteTo(writer);
    }

    public override bool CanRead
    {
        get { return false; }
    }

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

}

Then, use it like this:

var json = JsonConvert.SerializeObject(data, Formatting.Indented, new MetadataConverter());

Fiddle: https://dotnetfiddle.net/5IRDeu

Brian Rogers
  • 125,747
  • 31
  • 299
  • 300
-1

If you need it to serialize that way, try some post-processing on the serialized string. Keep in mind this won't deserialize well without some processing before deserialization.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft;
using Newtonsoft.Json.Linq;

namespace ConsoleApplication1
{

    public class Metadata
    {
        public string Name { get; set; }
        public Guid Id { get; set; }
    }

    public class Root
    {
        public List<Metadata> listOfMetadata{get;set;}
    }

    class Program
    {
        static void Main(string[] args)
        {
            Run run = new Run();
            run.SerializeCars();
        }
    }

    public class Run
    {
        public void SerializeCars()
        {
            var root = new Root()
            {
                listOfMetadata = new List<Metadata>()
                {
                    new Metadata
                    { 
                        Name = "MyName", Id = Guid.NewGuid()
                    }
                }
            };

            var json = JsonConvert.SerializeObject(root, Formatting.Indented);
            json = json.Replace("\"listOfMetadata\": ", string.Empty);
            Console.WriteLine(json);
            Console.ReadKey();
        }
    }
}
james
  • 712
  • 4
  • 11
  • 29
  • This solution does not generate the desired result as shown in the question. The OP wants the `Metadata` properties to end up inside separate objects in the array. – Brian Rogers Mar 24 '16 at 15:38