json.net

Newtonsoft JSON- Conversion to/from DataSet causes Decimal to become Double?

烈酒焚心 提交于 2019-12-10 15:57:46
问题 I'm using Newtonsoft JSON to serialize a DataSet to binary JSON using the code below. When de-serializing back to a DataSet, the field type changes from a Decimal to a Double? Does anybody know what's going wrong? Sample code: static void Main(string[] args) { var ds = new DataSet(); var dt = ds.Tables.Add(); dt.Columns.Add("Test", typeof(Decimal)); dt.Rows.Add(new object[] { 1.23345M }); var data = DataSetToBinJSON(ds); var ds2 = BinJSONToDataSet(data); Console.WriteLine((ds2.Tables[0]

How to let deserialization throw exception on non-integer where expecting an int?

廉价感情. 提交于 2019-12-10 15:56:24
问题 I am trying to parse a decimal value from json (eg. id: 4.5 ) to a poco int and I want an exception. Background: This deserialization throws Newtonsoft.Json.JsonSerializationException when encountering a decimal where expecting an int : httpContent.ReadAsAsync<MyCollection<T>>( mediaTypeFormatters, cancellationToken); MyCollection<T> is a class with a list Result of type T , and T can have an int . Now, I want to catch the ones that throw and keep the rest. So I first extract it as a

Json.NET different json structure, based on enum value

爱⌒轻易说出口 提交于 2019-12-10 15:47:40
问题 I need to convert my class to JSON and I use Json.NET. But I can have different JSON structures, like: { name: "Name", type: "simple1", value: 100 }; or { name: "Name", type: { optional1: { setting1: "s1", setting2: "s2", ///etc. }, value: 100 }; My C# code is: public class Configuration { [JsonProperty(PropertyName = "name")] public string Name{ get; set; } [JsonProperty(PropertyName = "type")] public MyEnumTypes Type { get; set; } public OptionalType TypeAdditionalData { get; set; }

Create a new AnonymousType instance

无人久伴 提交于 2019-12-10 15:42:08
问题 I'm trying to create an AnonymousType instance looks like: new { Channel = g.Key.Channel, Comment = g.Key.Comment, Count = g.Count() } On the dark, .NET creates an AnonymousType with a constructor that takes three arguments: String, String, Int32 . In order to create a new instance of this anonymous type, T, I do: object[] args = new object[3]; args[0] = "arg1"; args[1] = "arg2"; args[2] = 200; (T)Activator.CreateInstance(typeof(T), args); .NET dumps me: Additional information: Constructor

Additional text encountered after finished reading JSON content and Unexpected token , in my json

丶灬走出姿态 提交于 2019-12-10 15:35:46
问题 I am having some problems with my json-feed created with JSON.Net. When I try to parse it, it gives me Additional text encountered after finished reading JSON content: ,. Path '', line 17, position 4. I tried validating it with http://json.parser.online.fr/ and it says "SyntaxError: Unexpected token ,". Any ideas why ? I have pasted my json below: { "ReviewId": 10250, "DateOfVisit": "Wed, 04 Jan 2012 00:00:00 +01:00", "SmartDateOfVisit": "Wednesday, January 04, 2012 12:00 AM", "First": null,

How do I deserialize a missing property to a default value in Json.NET?

纵饮孤独 提交于 2019-12-10 15:27:01
问题 I have a class that is annotated with DataContract and DataMember attributes. Some members are marked as DataMember(IsRequired = true) . When I serialize instances over the wire from Json.NET, and the required object members have null value, then their serialized values are missing in the output (which is apparently equivalent to being null in JSON). I'm okay with that. I've created a sort of "echo" service which returns data sent to it as a response. So this service receives the JSON with

Serialize Dictionary without property name with Newtonsoft.Json

跟風遠走 提交于 2019-12-10 15:24:44
问题 I'm using var myResponse = new Response(myDictionary); string response = JsonConvert.SerializeObject(myResponse); where internal class Response { public Response (Dictionary<string, string> myDict) { MyDict = myDict; } public Dictionary<string, string> MyDict { get; private set; } } I'm getting: { "MyDict": { "key" : "value", "key2" : "value2" } } what I want to get is: { "key" : "value", "key2" : "value2" } ` is that possible with Newtonsoft.Json? 回答1: You're serializing the entire object.

Decode Html-encoded characters during Json deserialization

孤街浪徒 提交于 2019-12-10 15:13:25
问题 I’m using Json.net to deserialize json data received by Web API call. Some fields often have html-encoded characters like " or & How can I have this characters automatically decoded during deserialization? I came to 2 possible solutions: Calling System.Web.HttpUtility.HtmlDecode() in property setter like: public string Title { set { title = System.Web.HttpUtility.HtmlDecode(value); } } Writing custom JsonConverter that calls System.Web.HttpUtility.HtmlDecode() in ReadJson() method: public

ServiceStack.Text.JsonObject.Parse vs. NewtonSoft.Json.Linq.JObject.Parse for nested tree of 'dynamic' instances?

帅比萌擦擦* 提交于 2019-12-10 15:12:25
问题 I'd like to try ServiceStack's json parsing, but I've already figured out how to do something I need via Newtonsoft. Can this same thing by done via ServiceStack? I've tried with the commented out code but it gives exceptions, see below for exception details. Thanks! Josh [Test] public void TranslateFromGitHubToCommitMessage() { const string json = @" { 'commits': [ { 'author': { 'email': 'dev@null.org', 'name': 'The Null Developer' }, 'message': 'okay i give in' }, { 'author': { 'email':

JSON.NET CustomCreationConverter with nested objects

穿精又带淫゛_ 提交于 2019-12-10 15:08:53
问题 that is my very first question I ask on this site, so forgive me if I missed something. I have some problems deserializing an complex object graph using JSON.NET. My class hierarchy is (simplified) as follows: public abstract class BusinessObjectBase { protected BusinessObjectBase(SerializationContext context) { } } public class TestBusinessObject : BusinessObjectBase { protected TestBusinessObject(SerializationContext context) : base(context) { } public NestedObject InnerObject { get; set; }