Linear serialization and de-serialization

I am using JSON.NET and C # 5. I need to serialize / de-serialize a list of objects into a json delimited string. http://en.wikipedia.org/wiki/Line_Delimited_JSON . Example,

{"some":"thing1"} {"some":"thing2"} {"some":"thing3"} 

and

 {"kind": "person", "fullName": "John Doe", "age": 22, "gender": "Male", "citiesLived": [{ "place": "Seattle", "numberOfYears": 5}, {"place": "Stockholm", "numberOfYears": 6}]} {"kind": "person", "fullName": "Jane Austen", "age": 24, "gender": "Female", "citiesLived": [{"place": "Los Angeles", "numberOfYears": 2}, {"place": "Tokyo", "numberOfYears": 2}]} 

Why I need it because its requirement is Google BigQuery https://cloud.google.com/bigquery/preparing-data-for-bigquery

Update: One way I found is to serialize each individual object and concatenate at the end with a new line.

+6
source share
1 answer

You can do this by manually JsonTextReader JSON using JsonTextReader and setting the SupportMultipleContent flag to true .

If we look at your first example and create a POCO called Foo :

 public class Foo { [JsonProperty("some")] public string Some { get; set; } } 

Here's how we parse it:

 var json = "{\"some\":\"thing1\"}\r\n{\"some\":\"thing2\"}\r\n{\"some\":\"thing3\"}"; var jsonReader = new JsonTextReader(new StringReader(json)) { SupportMultipleContent = true // This is important! }; var jsonSerializer = new JsonSerializer(); while (jsonReader.Read()) { Foo foo = jsonSerializer.Deserialize<Foo>(jsonReader); } 
+12
source

Source: https://habr.com/ru/post/985453/


All Articles