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.
Related
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
This question already has answers here:
Serializing F# Record type to JSON includes '#' character after each property
(2 answers)
Closed 7 years ago.
I use F#, but I believe the question is not F# specific.
I have the interface for Web Service:
[<ServiceContract>]
type IRestService =
[<OperationContract>]
[<WebGet(UriTemplate = "Maintenance", ResponseFormat=WebMessageFormat.Json)>]
abstract GetMaintenancesRest: a:unit -> Maintenance[]
When I am trying to use this service I can get JSON, but all field's names in the JSON have the symbol '#':
[{"Address#":"one","Assetid#":"","Assignmentdate#":"/Date(1434147917730-0700)/","Comment#":"" ...
Why and how can I fix it?
It's F#. I ran into this a long time ago, so I might be forgetting. As I recall you may not use records to return from the service, or you get this error. You need class types with properties. I don't think mutable records work either. I also don't think public fields work; it's got to be public read/write properties.
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
This question already has answers here:
Exclude property from serialization via custom attribute (json.net)
(7 answers)
Closed 9 years ago.
I tried searching google and stackoverflow for this simple question but had no luck.
When using Json.NET I want to serialize an object with all of the fields except a specific type.
I don't want to mark every field/property with JsonIgnore to prevent serialization instead what I want to do is just mark the specific type as "NonSerialized". This of course won't work. Is there anyway to achieve it without marking properties and fields with attributes?
I tried resolving this using DefaultContractResolver but didn't manage to make it work.
You could try to use the JavaScriptSerializer class and implement a JavaScriptConverter which excludes the properties you don't need. You can then register that converter on the JavaScriptSerializer using its RegisterConverters method.
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?