Questions tagged [datacontractjsonserializer]

DataContractJsonSerializer is a .NET component that makes it possible to directly serialize .NET objects into JSON data and to deserialize such data back into instances of .NET types.

DataContractJsonSerializer is part of the Microsoft (version 3.5 onwards). It makes it possible to directly serialize .NET objects into data and to deserialize such data back into instances of .NET types.

As with (with which it shares an abstract base class XmlObjectSerializer), DataContractJsonSerializer is intended to serialize data contract types, thus appropriate data contract attributes may be applied to control the serialization of objects from and to JSON.

188 questions
179
votes
9 answers

How do I serialize a C# anonymous type to a JSON string?

I'm attempting to use the following code to serialize an anonymous type to JSON: var serializer = new DataContractJsonSerializer(thing.GetType()); var ms = new MemoryStream(); serializer.WriteObject(ms, thing); var json =…
25
votes
1 answer

DataContractJsonSerializer to skip nodes with null values

I am using DataContractJsonSerializer to serialize my custom object to JSON. But i want to skip the data members whose values are null. If DataMember is null that node should not come in JSON string. How can I achieve this? Give me a simple code…
15
votes
3 answers

Serialize Dictionary to JSON with DataContractJsonSerializer

I have an object tree that I'm serializing to JSON with DataContractJsonSerializer. Dictionary gets serialized but I don't like the markup - the items are not rendered like this: {key1:value, key2:value2} but rather like an array of…
14
votes
5 answers

DataContractJsonSerializer - Deserializing DateTime within List
I'm having trouble using the System.Runtime.Serialization.Json.DataContractJsonSerializer class to deserialize DateTime instances contained within a List. I cannot seem to get DateTime to deserialize back into the original type. The…
Matthew Ruston
  • 4,282
  • 7
  • 38
  • 47
11
votes
1 answer

DataContractJsonSerializer human-readable json

Basically a dupe of this question with one notable difference - I have to use DataContractJsonSerializer. A simple using (var stream = new MemoryStream()) { var serializer = new DataContractJsonSerializer(typeof(Person)); …
Sinatr
  • 20,892
  • 15
  • 90
  • 319
11
votes
2 answers

Proper way to handle the ampersand character in JSON string send to REST web service

OK, I am using the System.Runtime.Serialization and the DataContractJsonSerialization. The problem is that in the request I send a value of a property with the & character. Say, AT&T, and I get a response with error: Invalid JSON Data. I thought…
9
votes
1 answer

Deserialize json with C# .NET Core

Im trying to deserialize data that Ive got over POST in JSON format but having some problem. The error message is: SerializationException: Expecting state 'Element'.. Encountered 'Text' with name '', namespace ''. …
Michal Takáč
  • 1,005
  • 3
  • 17
  • 37
8
votes
2 answers

error deserializing the object of type .... End element '......' from namespace '' expected. Found element 'item' from namespace ''

When I deserialize my jsonstring ,I am getting error message There was an error deserializing the object of type RecordInfo. End element 'Warning' from namespace '' expected. Found element 'item' from namespace ''. This is my JsonString public…
Steve
  • 1,471
  • 7
  • 19
  • 32
7
votes
1 answer

DataContractJsonSerializer DateTime implicit timezone conversion

I have a date time in the database and I retrieve it from the database using Entity Framework, I then pass out the data via JSON API through the DataContractJsonSerializer. The time in the date time field appears to have been adjusted according to…
krisdyson
  • 3,217
  • 7
  • 43
  • 86
6
votes
2 answers

How to send JSON in C# with DataContractJsonSerializer without charset?

I use DataContractJsonSerializer and StringContent to send JSON to a web service: DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Employee)); MemoryStream ms = new MemoryStream(); serializer.WriteObject(ms,…
royco
  • 5,409
  • 13
  • 60
  • 84
5
votes
2 answers

JSON sample to [DataContract]

Is there any tool allowing to generate DataContract entity definitions based on a JSON sample? I'm really missing the old good WSDL metadata with code generation facilities.
Ivan G.
  • 5,027
  • 2
  • 37
  • 65
5
votes
2 answers

How can I serialize anonymous type using DataContractJsonSerializer

Using the code below, how can I use the DataContractJsonSerializer to convert the resultant list to JSON? ETA: I have seen several extension methods to generically JSON-ize objects, but I'm not sure what type to use, as this is an anonymous…
Jon
  • 51
  • 1
  • 3
5
votes
1 answer

HL7 FHIR serialisation to json in asp.net web api

I'm using the HL7.Fhir nuget package 0.9.3 created by Ewout Kramer. I am tying it up with ASP.NET Web API, but unfortunately the built in JSON serialization isn't generating the JSON correctly. It contains lots of…
5
votes
2 answers

Partial deserialization of JSON object by using DataContractJsonSerializer

As a response from a Bitbucket REST API I'm getting the following JSON object (simplified version): { "repositories": [ { "scm": "hg", "has_wiki": false, "language": "c#", …
4
votes
1 answer

Safest place for the XmlSerializer to save temp files

It's come to my attention that the XmlSerializer needs to use disk space to do its bidding. If there is no writeable %temp% folder, then it fails with an error as follows: Source : System.Xml Message : Unable to generate a temporary class…
spender
  • 117,338
  • 33
  • 229
  • 351
1
2 3
12 13