I'm using newtonsoft in my .net core project and came across a particular json format.
I've got a list and the 'normal' json that come out after serialization is something like this:
{
"id": "0124",
"entities": [
{
"name": "chris",
"surname": "green"
},
{
"name": "albert",
"surname": "red"
}
]
}
I need to transform it in something like:
{
"id": "0124",
"entities": [
{
"chris": "green",
"albert": "red"
}
]
}
I don't know how many entities there could be inside my list.
Thank you all
EDIT
Thanks for the help guys, so this is the code:
var jsonBody = JsonConvert.SerializeObject(
new BulkRegisterDto
{
Id = "0124",
Entities = entities
}
);
public class BulkRegisterDto
{
[JsonProperty(PropertyName = "id")]
public string Id{ get; set; }
[JsonProperty(PropertyName = "entities")]
public IList<Person> Entities { get; set; }
}
public class Person
{
public string Name { get; set; }
public string Surname { get; set; }
}
These are not the real properties. I'm trying to use a dynamic JObject. We will see if it works
#Aleks Andreev I set your answer as the right one because it solves the problem but i prefer a better approach and I created my custom json converter using #MindSwipe comment.
This is my code below:
public class BulkEntityConverter : JsonConverter
{
public override object ReadJson(
JsonReader reader,
Type objectType,
object existingValue,
JsonSerializer serializer)
{
var obj = (JObject)JObject.ReadFrom(reader);
JProperty property = obj.Properties().FirstOrDefault();
// I didn't use this method
return new BulkRegisterDto
{
Id = property.Name,
Entities = new List<Person>()
};
}
public override void WriteJson(
JsonWriter writer,
object value,
JsonSerializer serializer)
{
BulkRegisterDto permission = (BulkRegisterDto)value;
var innerEntities = new JObject();
foreach (var entry in permission.Entities)
{
innerEntities.Add(entry.Name, entry.Surname);
}
var root = new JObject
{
{ "id", permission.Id},
{ "entities", new JArray { innerEntities } }
};
root.WriteTo(writer);
}
public override bool CanConvert(Type t)
{
return typeof(BulkRegisterDto).IsAssignableFrom(t);
}
public override bool CanRead
{
get { return true; }
}
}
Using this way i can get a clear class where to use my json converter like:
var jsonBody = JsonConvert.SerializeObject(manager, new BulkEntityConverter());
where manager is my BulkRegisterDto object
Thanks for all the support guys
You can deserialize your json to DTO:
[DataContract]
private class Person
{
[DataMember(Name = "name")]
public string Name { get; set; }
[DataMember(Name = "surname")]
public string Surname { get; set; }
}
[DataContract]
private class Entities
{
[DataMember(Name = "entities")]
public Person[] Persons { get; set; }
}
and then build expected json with raw JObject and JArray
var input = "{\r\n \"entities\": [\r\n {\r\n \"name\": \"chris\",\r\n \"surname\": \"green\"\r\n },\r\n {\r\n \"name\": \"albert\",\r\n \"surname\": \"red\"\r\n }\r\n ]\r\n}";
var json = JsonConvert.DeserializeObject<Entities>(input);
var root = new JObject();
var innerObject = new JObject();
root.Add("entities", new JArray {innerObject});
foreach (var entry in json.Persons)
innerObject.Add(entry.Name, entry.Surname);
If you call root.ToString() you will get
{
"entities": [
{
"chris": "green",
"albert": "red"
}
]
}
Related
I am using JsonSerializer to serialize/deserialize a class and it is working well.
But in this class, there is a list that I want to serialize but not for every elements in the list.
There is 3 type in this list with inheritance:
FileInformation and FolderInformation that both inherit from TreeViewElement.
How can I filter depending on the type? I want to serialize all FolderInformations Instance but not FileInformations.
You can use a JsonConverter attribute on your list property to filter the list during serialization.
Here's an example I wrote in LINQPad:
void Main()
{
var document = new Document
{
Id = 123,
Properties = {
new Property { Name = "Filename", Value = "Mydocument.txt" },
new Property { Name = "Length", Value = "1024" },
new Property {
Name = "My secret property",
Value = "<insert world domination plans here>",
IsSerializable = false
},
}
};
var json = JsonConvert.SerializeObject(document, Formatting.Indented).Dump();
var document2 = JsonConvert.DeserializeObject<Document>(json).Dump();
}
public class Document
{
public int Id { get; set; }
[JsonConverterAttribute(typeof(PropertyListConverter))]
public List<Property> Properties { get; } = new List<Property>();
}
public class Property
{
[JsonIgnore]
public bool IsSerializable { get; set; } = true;
public string Name { get; set; }
public string Value { get; set; }
}
public class PropertyListConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(List<Property>);
}
public override object ReadJson(JsonReader reader, Type objectType,
object existingValue, JsonSerializer serializer)
{
var list = (existingValue as List<Property>) ?? new List<Property>();
list.AddRange(serializer.Deserialize<List<Property>>(reader));
return list;
}
public override void WriteJson(JsonWriter writer, object value,
JsonSerializer serializer)
{
var list = (List<Property>)value;
var filtered = list.Where(p => p.IsSerializable).ToList();
serializer.Serialize(writer, filtered);
}
}
The output:
{
"Id": 123,
"Properties": [
{
"Name": "Filename",
"Value": "Mydocument.txt"
},
{
"Name": "Length",
"Value": "1024"
}
]
}
You would have to adapt your attribute to your own types and filtering criteria but this should get you started.
You can use the [JsonIgnore] Attribute, to ignore the whole property
Like this:
class Cls
{
public string prop1 { get; set; }
public string prop2 { get; set; }
[JsonIgnore]
public string prop3 { get; set; }
}
class Program
{
static void Main(string[] args)
{
var cls = new Cls
{
prop1 = "lorem",
prop2 = "ipsum",
prop3 = "dolor"
};
System.Console.WriteLine(JsonConvert.SerializeObject(cls));
//Output: {"prop1":"lorem","prop2":"ipsum"}
}
}
EDIT: For ignoring only the value.
If its only the value, needs to be ignored and not the whole property:
class Cls
{
public List<string> prop1 { get; set; }
public List<string> prop2 { get; set; }
public List<string> prop3 { get; set; }
[OnSerializing()]
internal void OnSerializingMethod(StreamingContext context)
{
prop3 = null;
}
}
class Program
{
static void Main(string[] args)
{
var cls = new Cls
{
prop1 = new List<string>{ "lorem", "ipsum", "dolor" },
prop2 = new List<string>{ "lorem", "ipsum", "dolor" },
prop3 = new List<string>{ "lorem", "ipsum", "dolor" },
};
System.Console.WriteLine(JsonConvert.SerializeObject(cls)); //Output: {"prop1":["lorem"],"prop2":["lorem"],"prop3":null}
}
}
I want to deserialize (and afterwards serialize again) the following Json
{
"id": "12345",
"custom_fields": [
{
"definition": "field1",
"value": "stringvalue"
},
{
"definition": "field2",
"value": [ "arrayvalue1", "arrayvalue2" ]
},
{
"definition": "field3",
"value": {
"type": "user",
"id": "1245"
}
}
]
}
The type of the value if different (field1: string, field2: array, field3: an extra nested structure).
This what I have:
public class CustomField
{
public string Definition { get; set; }
public object Value { get; set; }
}
public class RootObject
{
public string Id { get; set; }
public List<CustomField> Custom_fields { get; set; }
}
The Value-field is defined as object, but this is a problem when serializing again, especially the nested structure for field3.
How can I solve this?
You could implement a custom converter for your Value property:
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
namespace Test
{
public class Program
{
static void Main(string[] args)
{
var json =
#"{
""id"": ""12345"",
""custom_fields"": [
{
""definition"": ""field1"",
""value"": ""stringvalue""
}, {
""definition"": ""field2"",
""value"": [ ""arrayvalue1"", ""arrayvalue2"" ]
}, {
""definition"": ""field3"",
""value"": {
""type"": ""user"",
""id"": ""1245""
}
}
]
}";
var rootObject = JsonConvert.DeserializeObject<RootObject>(json);
Console.WriteLine(JsonConvert.SerializeObject(rootObject));
}
}
public class RootObject
{
[JsonProperty(PropertyName = "id")]
public string Id { get; set; }
[JsonProperty(PropertyName = "custom_fields")]
public List<CustomField> CustomFields { get; set; }
}
public class CustomField
{
[JsonProperty(PropertyName = "definition")]
public string Definition { get; set; }
[JsonProperty(PropertyName = "value")]
[JsonConverter(typeof(CustomValueConverter))]
public dynamic Value { get; set; }
}
public class CustomValueConverter : JsonConverter
{
// By returning false, we let the default `WriteJson` kick in
public override bool CanWrite => false;
public override bool CanConvert(Type objectType)
{
return true;
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null)
{
return string.Empty;
}
if (reader.TokenType == JsonToken.String)
{
return serializer.Deserialize<string>(reader);
}
if (reader.TokenType == JsonToken.StartArray)
{
return serializer.Deserialize<string[]>(reader);
}
return serializer.Deserialize<Dictionary<string, string>>(reader);
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
}
I have following JSon and I am using Json.NET (Newtonsoft.Json):
{
"total_items": "62",
"page_number": "6",
"page_size": "10",
"page_count": "7",
"cars": {
"car": [
{
"car_name": "Honda",
"engines": {
"engine": [
{
"name": "1.2L"
},
{
"name": "1.8L"
}
]
},
"country": "Japan"
},
{
"car_name": "Ford",
"engines": {
"engine": {
"name": "2.2L"
}
},
"country": "Japan"
},
{
"car_name": "VW",
"engines": null,
"country": "Germany"
}
]
}
}
And I have following Car object:
class Car
{
public Car() { }
public string Name { get; set; }
public string Country { get; set; }
public List<String> EngineNames { get; set; }
}
I need to handle case if "engines=null". If it is not null, then get all engine names. So, for example above, my EngineNames list for Honda and VW would be:
Honda.EngineNames = {"1.2L", "1.8L"} // has 2 names
VW.EngineNames = null //has nothing since no engine is provided
I need to pars the JSON above to get car data. I am parsing car_name and country but I dont know how to parse all engine names in array of engines (array could be null).
private Cars GetCars(string json)
{
dynamic data = (JObject)JsonConvert.DeserializeObject(json);
foreach (dynamic d in data.cars.car)
{
Car c = new Car();
c.Name = (string)d.SelectToken("car_name");
c.Country = (string)d.SelectToken("country");
c.EngineNames = //HOW TO GET ALL ENGINE NAMES AND HANDLE NULL ?
CarList.Add(c);
}
return CarList;
}
The best approach is to cut this dynamic a-la-javascript kind of crap out and just define your strongly typed models that will match your JSON structure:
public class Wrapper
{
public Cars Cars { get; set; }
}
public class Cars
{
public Car[] Car { get; set; }
}
public class Car
{
[JsonProperty(PropertyName = "car_name")]
public string Name { get; set; }
public string Country { get; set; }
public Engines Engines { get; set; }
}
public class Engines
{
public Engines()
{
Engine = new Engine[0];
}
// We need to use a custom JSON converter
// because of this pretty broken schema that you have
// in which the engine property can be array and a standard
// object at the same time
[JsonConverter(typeof(EnginesConverter))]
public Engine[] Engine { get; set; }
}
public class Engine
{
public string Name { get; set; }
}
and then just let JSON.NET do the magic of converting this string back into your strongly typed objects:
var wrapper = JsonConvert.DeserializeObject<Wrapper>(json);
and now that you have a strongly typed structure, you could more than easily map this to a desired C# DTO using LINQ:
public class CarsDto
{
public CarsDto()
{
Engines = new List<string>();
}
public string Name { get; set; }
public string Country { get; set; }
public List<string> Engines { get; set; }
}
and then:
var dto = wrapper.Cars.Car.Select(c => new CarsDto
{
Name = c.Name,
Country = c.Country,
Engines = (c.Engines ?? new Engines()).Engine.Select(e => e.Name).ToList(),
}).ToList();
and finally here's the custom JSON converter that we used:
public class EnginesConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return true;
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.StartArray)
{
return serializer.Deserialize<Engine[]>(reader);
}
else
{
Engine e = serializer.Deserialize<Engine>(reader);
return new[] { e };
}
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
Why not continue using dynamic typing, and access object properties as dynamics, so you can do:
var car = new Car();
car.Name = (string)d.car_name;
car.EngineNames = (d.engines != null ? ((IEnumerable)d.engines).Cast<dynamic>().Select(e => (string)e.name) : null);
The fields are getting ignored. I'm successfully getting back a list of 2 objects, but none of the fields are populated. What am I doing wrong? (Unfortunately I don't have control over the json format. It arrives in exactly this structure.
using System.Web.Script.Serialization;
public void myMethod {
string myContent = #"
[
{
"my_object": {
"city": "city 1",
"myAge": 15
}
},
{
"my_object": {
"city": "city 2",
"myAge": 18
}
}
]";
JavaScriptSerializer serializer = new JavaScriptSerializer();
List<my_object> list = serializer.Deserialize<List<my_object>>(myContent);
}
public class json_content {
public string city { get; set; }
public int myAge { get; set; }
}
This code solves your problem:
public void myMethod()
{
string myContent = #"
[
{
""json_content"": {
""city"": ""city 1"",
""myAge"": 15
}
},
{
""json_content"": {
""city"": ""city 2"",
""myAge"": 18
}
}
]";
JavaScriptSerializer serializer = new JavaScriptSerializer();
List<wrapper> list = serializer.Deserialize<List<wrapper>>(myContent);
}
public class wrapper
{
public json_content json_content { get; set; }
}
public class json_content
{
public string city { get; set; }
public int myAge { get; set; }
}
You have list of objects with one property json_content in your JSON, but expect list directly containing json_content objects.
Most likely fix is to remove intermediate object from JSON (if you control it):
[
{
"city": "city 1",
"myAge": 15
},...
];
If you don't control JSON add outer class:
class JsonOuterContent
{
public JsonContent json_content;
}
List<JsonOuterContent> list = serializer
.Deserialize<List<JsonOuterContent>>(myContent);
In my web app I'm using Newtonsoft.Json and I have following object
[Newtonsoft.Json.JsonObject(Title = "MyCar")]
public class Car
{
[Newtonsoft.Json.JsonProperty(PropertyName = "name")]
public string Name{get;set;}
[Newtonsoft.Json.JsonProperty(PropertyName = "owner")]
public string Owner{get;set;}
}
and I want serialize them with root name (class name). This is desired format using
{'MyCar':
{
'name': 'Ford',
'owner': 'John Smith'
}
}
I know that I can do that with anonymous object, but is any property or another way in Newtonsoft.Json library?
Use anonymous class
Shape your model the way you want using anonymous classes:
var root = new
{
car = new
{
name = "Ford",
owner = "Henry"
}
};
string json = JsonConvert.SerializeObject(root);
I found an easy way to render this out... simply declare a dynamic object and assign the first item within the dynamic object to be your collection class...This example assumes you're using Newtonsoft.Json
private class YourModelClass
{
public string firstName { get; set; }
public string lastName { get; set; }
}
var collection = new List<YourModelClass>();
var collectionWrapper = new {
myRoot = collection
};
var output = JsonConvert.SerializeObject(collectionWrapper);
What you should end up with is something like this:
{"myRoot":[{"firstName":"John", "lastName": "Citizen"}, {...}]}
You can easily create your own serializer
var car = new Car() { Name = "Ford", Owner = "John Smith" };
string json = Serialize(car);
string Serialize<T>(T o)
{
var attr = o.GetType().GetCustomAttribute(typeof(JsonObjectAttribute)) as JsonObjectAttribute;
var jv = JValue.FromObject(o);
return new JObject(new JProperty(attr.Title, jv)).ToString();
}
Sorry, my english is not that good. But i like to improve the upvoted answers.
I think that using Dictionary is more simple and clean.
class Program
{
static void Main(string[] args)
{
agencia ag1 = new agencia()
{
name = "Iquique",
data = new object[] { new object[] {"Lucas", 20 }, new object[] {"Fernando", 15 } }
};
agencia ag2 = new agencia()
{
name = "Valparaiso",
data = new object[] { new object[] { "Rems", 20 }, new object[] { "Perex", 15 } }
};
agencia agn = new agencia()
{
name = "Santiago",
data = new object[] { new object[] { "Jhon", 20 }, new object[] { "Karma", 15 } }
};
Dictionary<string, agencia> dic = new Dictionary<string, agencia>
{
{ "Iquique", ag1 },
{ "Valparaiso", ag2 },
{ "Santiago", agn }
};
string da = Newtonsoft.Json.JsonConvert.SerializeObject(dic);
Console.WriteLine(da);
Console.ReadLine();
}
}
public class agencia
{
public string name { get; set; }
public object[] data { get; set; }
}
This code generate the following json (This is desired format)
{
"Iquique":{
"name":"Iquique",
"data":[
[
"Lucas",
20
],
[
"Fernando",
15
]
]
},
"Valparaiso":{
"name":"Valparaiso",
"data":[
[
"Rems",
20
],
[
"Perex",
15
]
]
},
"Santiago":{
"name":"Santiago",
"data":[
[
"Jhon",
20
],
[
"Karma",
15
]
]
}
}
string Json = JsonConvert.SerializeObject(new Car { Name = "Ford", Owner = "John Smith" }, Formatting.None);
for the root element use GlobalConfiguration.
A very simple approach for me is just to create 2 classes.
public class ClassB
{
public string id{ get; set; }
public string name{ get; set; }
public int status { get; set; }
public DateTime? updated_at { get; set; }
}
public class ClassAList
{
public IList<ClassB> root_name{ get; set; }
}
And when you going to do serialization:
var classAList = new ClassAList();
//...
//assign some value
//...
var jsonString = JsonConvert.SerializeObject(classAList)
Lastly, you will see your desired result as the following:
{
"root_name": [
{
"id": "1001",
"name": "1000001",
"status": 1010,
"updated_at": "2016-09-28 16:10:48"
},
{
"id": "1002",
"name": "1000002",
"status": 1050,
"updated_at": "2016-09-28 16:55:55"
}
]
}
Hope this helps!
Well, you can at least tell Json.NET to include the type name: http://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_TypeNameHandling.htm . Newtonsoft.Json.JsonSerializer jser = new Newtonsoft.Json.JsonSerializer();
jser.TypeNameHandling = TypeNameHandling.Objects;
The type will be included at the beginning in the "$type" property of the object.
This is not exactly what you are looking for, but it was good enough for me when facing a similiar problem.
Writing a custom JsonConverter is another approach mentioned in similar questions. However, due to nature of how JsonConverter is designed, using that approach for this question is tricky, as you need to be careful with the WriteJson implementation to avoid getting into infinite recursion: JSON.Net throws StackOverflowException when using [JsonConvert()].
One possible implementation:
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
//JToken t = JToken.FromObject(value); // do not use this! leads to stack overflow
JsonObjectContract contract = (JsonObjectContract)serializer.ContractResolver.ResolveContract(value.GetType());
writer.WriteStartObject();
writer.WritePropertyName(value.GetType().Name);
writer.WriteStartObject();
foreach (var property in contract.Properties)
{
// this removes any property with null value
var propertyValue = property.ValueProvider.GetValue(value);
if (propertyValue == null) continue;
writer.WritePropertyName(property.PropertyName);
serializer.Serialize(writer, propertyValue);
//writer.WriteValue(JsonConvert.SerializeObject(property.ValueProvider.GetValue(value))); // this adds escaped quotes
}
writer.WriteEndObject();
writer.WriteEndObject();
}
I hope this help.
//Sample of Data Contract:
[DataContract(Name="customer")]
internal class Customer {
[DataMember(Name="email")] internal string Email { get; set; }
[DataMember(Name="name")] internal string Name { get; set; }
}
//This is an extension method useful for your case:
public static string JsonSerialize<T>(this T o)
{
MemoryStream jsonStream = new MemoryStream();
var serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(T));
serializer.WriteObject(jsonStream, o);
var jsonString = System.Text.Encoding.ASCII.GetString(jsonStream.ToArray());
var props = o.GetType().GetCustomAttributes(false);
var rootName = string.Empty;
foreach (var prop in props)
{
if (!(prop is DataContractAttribute)) continue;
rootName = ((DataContractAttribute)prop).Name;
break;
}
jsonStream.Close();
jsonStream.Dispose();
if (!string.IsNullOrEmpty(rootName)) jsonString = string.Format("{{ \"{0}\": {1} }}", rootName, jsonString);
return jsonString;
}
//Sample of usage
var customer = new customer {
Name="John",
Email="john#domain.com"
};
var serializedObject = customer.JsonSerialize();
[Newtonsoft.Json.JsonObject(Title = "root")]
public class TestMain
this is the only attrib you need to add to get your code working.