Formatting output of JavaScriptSerializer [duplicate] - c#

This question already has answers here:
How to set formatting with JavaScriptSerializer when JSON serializing?
(4 answers)
Closed 7 years ago.
I am using JavaScriptSerializer for a list - and it works:
string codeString = (new JavaScriptSerializer()).Serialize(ramGraphList);
Although the output is formatted as follows:
[["Time","Value"],["08/07/2012 12:43:28","5270"],["08/07/2012 12:44:32","5277"]];
The problem is that the values "5270" and "5277" are not strings, they should be treated as int's hence they have to be unquoted.
Is there an effective way of achieve this?
Expected output:
[["Time","Value"],["08/07/2012 12:43:28",5270],["08/07/2012 12:44:32",5277]];

I believe that it's not possible to do this with the JavaScriptSerializer class without wraping it or modifying the class. However, have you considered using DataContractJsonSerializer or JSON.Net?

Related

Is there any way to get my json without creating a class [duplicate]

This question already has answers here:
How do you read a simple value out of some json using System.Text.Json?
(8 answers)
Closed 11 months ago.
From an API call I am getting some JSON data, I dont want to declare a class, while in NewtonSoft we can get those data as given below without creating a class,
JObject o1 = JObject.Parse(File.ReadAllText(#"example.json"));
Console.WriteLine(o1["customername"]);
Is there any similar way to get json data without creating a class in System.Text.Json
Edit:
I was able to proceed by the solutions provided here
How do you read a simple value out of some json using System.Text.Json?
You could try using JSON document model and parse subsections you are interested in.
See this link https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-use-dom-utf8jsonreader-utf8jsonwriter?pivots=dotnet-6-0#deserialize-subsections-of-a-json-payload

How to avoid Newtonsoft JObject.Parse type conversion [duplicate]

This question already has answers here:
Json.NET Disable the deserialization on DateTime
(2 answers)
JToken: Get raw/original JSON value
(3 answers)
JObject.Parse modifies end of floating point values
(3 answers)
Closed 2 years ago.
When I parse a String that contains a valid RFC3339 datetimestring, JOject.Parse() automatically makes an implizit type conversion to DateTime:
String toParse = "{\"datetime\": \"2020-08-04T23:45:00+02:00\"}";
JObject p = JObject.Parse(toParse);
String d = (String)p["datetime"];
Console.Write(d);
// Gives 08/04/2020 23:45:00
// But I want 2020-08-04T23:45:00+02:00
I think the "unwanted" typecast takes place while reading the p["datetime"] and not during JOject.Parse(), but how can I totally avoid that behavior and let it only deserialize everything as string?
I have this problem with different types, also decimals which are automatically casted, by I need them as String.
Thank you!

JsonConvert - Ignore [PropertyName("FooBar")]? [duplicate]

This question already has answers here:
How to ignore JsonProperty(PropertyName = "someName") when serializing json?
(2 answers)
Closed 5 years ago.
Is there a way to serialize a object ignoring the [PropertyName("FooBar")]?
Details: My object has enormous property names that are read all over the frontend... the problem is that it causes me to waste about 35% of space... when you take caching in consideration, this is a huge problem
You can accomplish this by creating a contract resolver, which inherits from DefaultContractResolver, and using that resolver in the front end where you want to ignore the attributes.

What is the C# Equivalent of java's transient keyword? [duplicate]

This question already has answers here:
How to exclude some members from being serialized to Json?
(3 answers)
Closed 7 years ago.
I'm using System.Web.Script.Serialization.JavaScriptSerializer to serialize an object to JSON, but there are some fields within the object which don't need to be serialized. In java I would mark them with the 'transient' keyword. Is there an equivalent keyword or standard practice in C#?
Put [ScriptIgnore] attribute on the property and it will not be serialized.
See: ScriptIgnoreAttribute Class

Deserialize JSON in C# without creating a class [duplicate]

This question already has answers here:
Deserialize JSON object into dynamic object using Json.net
(8 answers)
Closed 7 years ago.
I'm looking for a way to deserialize a string coming from an API. The Json string is just : {"key" : "lolo"}.
I dont want to create a class for this.
I have found this but it's kind of old so I don't know if there is something better today: Json deserialize
In the future I will use more Json deserialization so I would like to use JSON.NET.
Thanks.
Have a look at this: http://www.newtonsoft.com/json.
Used in almost every .net appilication these days.

Categories