json.net

How to serialize\deserialize a property where the property name is dependent on the data

泪湿孤枕 提交于 2020-01-03 17:47:47
问题 I'm trying to get the following json (see below) deserialized (using newtonsoft json serializer) and the problem is the variable named "2010-12" it is obviously dependent on the data returned - it represents a month and next month the value will change to "2010-01". Any ideas on how i could handle this with the following class? [JsonObject(MemberSerialization.OptIn)] public class Crimes { [JsonProperty()] public Month Month { get; set; } } Example JSON instance: { "commentary": null, "crimes"

How to serialize\deserialize a property where the property name is dependent on the data

筅森魡賤 提交于 2020-01-03 17:47:28
问题 I'm trying to get the following json (see below) deserialized (using newtonsoft json serializer) and the problem is the variable named "2010-12" it is obviously dependent on the data returned - it represents a month and next month the value will change to "2010-01". Any ideas on how i could handle this with the following class? [JsonObject(MemberSerialization.OptIn)] public class Crimes { [JsonProperty()] public Month Month { get; set; } } Example JSON instance: { "commentary": null, "crimes"

Deserialize JSON Object to .Net object with NewtonSoft

孤人 提交于 2020-01-03 16:51:23
问题 I got a json Object that i want to deserialize to its .Net type without casting it. I think i read somewhere in the doc that you can pass an attribute into the json to tells to the deserializer the .Net object type that it can try to cast. I can't find the where i read this. I want to avoid use of var myNewObject = JsonConvert.DeserializeObject<MyClass>(json); To get something like this MyClass myNewObject = JsonConvert.DeserializeObject(json); I got my json object from an HttpRequest and

What is the Json.NET Mono assembly reference?

孤街醉人 提交于 2020-01-03 16:02:58
问题 I am trying to compile this Json.NET code: using Newtonsoft.Json; ... MyDesc d = JsonConvert.DeserializeObject<MyDesc>(jsonInput); ... with this command via mono (on ubuntu): $ mcs Main.cs -lib:/home/username/JsonNET/Net40/Newtonsoft.Json.dll But i am getting "no assembly reference"-error: error CS0246: The type or namespace name `Newtonsoft' could not be found. Are you missing an assembly reference What is the correct Json.NET Mono assebmly reference? (-lib option looks right for this, but

What is the Json.NET Mono assembly reference?

本小妞迷上赌 提交于 2020-01-03 16:00:13
问题 I am trying to compile this Json.NET code: using Newtonsoft.Json; ... MyDesc d = JsonConvert.DeserializeObject<MyDesc>(jsonInput); ... with this command via mono (on ubuntu): $ mcs Main.cs -lib:/home/username/JsonNET/Net40/Newtonsoft.Json.dll But i am getting "no assembly reference"-error: error CS0246: The type or namespace name `Newtonsoft' could not be found. Are you missing an assembly reference What is the correct Json.NET Mono assebmly reference? (-lib option looks right for this, but

Disable Support for Reading (Invalid JSON) Single Quote Strings

橙三吉。 提交于 2020-01-03 09:00:11
问题 Newtonsoft.Json for C# supports reading stuff like {'key':'value'} but thats improper JSON. Is it possible to disable that so it parses and reads more like PHP (Where'as PHP doesnt support {'key':'value'} but {"key":"value"}) 回答1: You could write your own JsonReader subclass to perform this, but the JsonTextReader class (which is the most commonly used one, as far as I'm aware) doesn't support this. From the ParseValue method, for example: case '"': case '\'': ParseString(currentChar,

Json.Net boolean parsing issue

旧巷老猫 提交于 2020-01-03 07:59:07
问题 JObject.Parse(jsonString) is causing issue for boolean data. e.g. The json is : { "BoolParam": true } I used the following code to parse: JObject data = JObject.Parse(str1); foreach (var x in data) { string name = x.Key; Console.Write(name + " ("); JToken value = x.Value; Console.Write(value.Type + ")\n"); Console.WriteLine(value); } This print out the value as : BoolParam (Boolean) : True The case sensitivity causes issue as I save this json for later use. The saved json looks like {

An exception in Json.NET: Token PropertyName in state “Start” would result in an invalid JavaScript object

筅森魡賤 提交于 2020-01-03 07:39:25
问题 I see the error in my prod server log; can u give me any hint about what kinds of situations would trigger the error? Thanks. Token PropertyName in state Start would result in an invalid JavaScript object. 2010-08-02 04:33:56,446 DEBUG 10 XXX - at Newtonsoft.Json.JsonWriter.AutoComplete(JsonToken tokenBeingWritten) at Newtonsoft.Json.JsonWriter.WritePropertyName(String name) 回答1: Looking at the source: http://json.codeplex.com/SourceControl/changeset/view/64750#128137 It would appear you are

Custom Json Converter with dependecy

时光怂恿深爱的人放手 提交于 2020-01-03 03:56:06
问题 I have to use a custom JsonConverter with ASP.NET Core for a reason, and I need to use it with JsonInputFormatter . The only way I've found is to use AddJsonOption extension method like this: services .AddMvc() .AddJsonOptions(jso => jso.SerializerSettings.Converters.Add(new CustomConverter())) But it has a flaw: CustomConverter requires a dependency from a DI container which cannot be easily solved at configuration time. So the question: is there any programmer friendly way to supply a

Can I Use Json.Net de-serialization by default with ASP.net Web Api and MVC 3?

自作多情 提交于 2020-01-03 03:43:06
问题 I have a MVC 3 web application/ API that I am using and I am woefully tired of the default Microsoft Serialization. The big promise of the new ASP.net Web Api in MVC 4 is that it will use JSON.net de-serialization by default. Well I would love to use JSON deserialization by default and not mess with the WCF client. One issue I do have is I cannot convert to MVC 4 for various reasons. So my question is can I still use the Json.net deserialization by default with MVC 3? What I am trying to do