How to return JSon object

First of all, there’s no such thing as a JSON object. What you’ve got in your question is a JavaScript object literal (see here for a great discussion on the difference). Here’s how you would go about serializing what you’ve got to JSON though: I would use an anonymous type filled with your results type: … Read more

Deserialize array of key value pairs using Json.NET

The simplest way is deserialize array of key-value pairs to IDictionary<string, string>: public class SomeData { public string Id { get; set; } public IEnumerable<IDictionary<string, string>> Data { get; set; } } private static void Main(string[] args) { var json = “{ \”id\”: \”123\”, \”data\”: [ { \”key1\”: \”val1\” }, { \”key2\” : \”val2\” } … Read more

Using JSON.net, how do I prevent serializing properties of a derived class, when used in a base class context?

I use a custom Contract Resolver to limit which of my properties to serialize. This might point you in the right direction. e.g. /// <summary> /// json.net serializes ALL properties of a class by default /// this class will tell json.net to only serialize properties if they MATCH /// the list of valid columns passed … Read more

Convert an int to bool with Json.Net [duplicate]

Ended up creating a converter: public class BoolConverter : JsonConverter { public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { writer.WriteValue(((bool)value) ? 1 : 0); } public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { return reader.Value.ToString() == “1”; } public override bool CanConvert(Type objectType) { return objectType == typeof(bool); } … Read more

How to access elements of a JArray (or iterate over them)

There is a much simpler solution for that. Just treat the items of the JArray as JObject. Let’s say we have such array of JSON objects: JArray jArray = JArray.Parse(@”[ { “”name””: “”Croke Park II””, “”url””: “”http://twitter.com/search?q=%22Croke+Park+II%22″”, “”promoted_content””: null, “”query””: “”%22Croke+Park+II%22″”, “”events””: null }, { “”name””: “”Siptu””, “”url””: “”http://twitter.com/search?q=Siptu””, “”promoted_content””: null, “”query””: “”Siptu””, “”events””: null … Read more

Ignoring null fields in Json.net

Yes you need to use JsonSerializerSettings.NullValueHandling = NullValueHandling.Ignore. But because structs are value types you need to mark Field2, Field3 nullable to get the expected result: public struct structA { public string Field1; public structB? Field2; public structB? Field3; } Or just use classes instead of structs. Documentation: NullValueHandling Enumeration

Default value for missing properties with JSON.net

I found the answer, just need to add the following attribute as well: [JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)] In your example: class Cat { public Cat(string name, int age) { Name = name; Age = age; } public string Name { get; private set; } [DefaultValue(5)] [JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)] public int Age { get; private set; } … Read more

Instead of using reflection to loop though a class properties, can i serialize it to json string and look through it? Are there any downsides to it?

If you are disturbed by performance hit of the reflection you can “cache” it using generic static classes and private static fields in them (which are not shared) and some compilation magic using expression trees. For example (modified your code to make it compile): public static class Validator<T> { private static readonly Dictionary<string, Func<T, object>> … Read more

tech