0

I have a object which looks like this e.g. (its in a list of objects)

{
  name: "Freddy",
  Age: 25,
  animals: {
             dogname: "Woofa",
             dogname2: "bark"}
}

I have a list of these objects which look similar to how the above is structured. What I want to do is serialize the whole list of objects like so:

line1: {name:"Freddy", Age: 25, animals:{dogname: "Woofa", dogname2: "bark"}}

Now I can get it in a none formatted format, but essentially what I need is something like:

line1: {name:"Freddy", Age: 25, animals:{dogname: "Woofa", dogname2: "bark"}}
line2: {name:"Sam", Age: 56, animals:{dogname: "sludge"}}
......

and so on. What I am doing is serialising to a JSON file if that adds to more background information. How do I get each list when it is serialised to have a newline, between each object in the file.

Shiva
  • 20,575
  • 14
  • 82
  • 112
  • Why not write each record separately? – ProgrammingLlama Mar 22 '19 at 00:50
  • @John, how fast would that be? when I serialize the list it doesn't take that long. Would adding a `foreach` loop make it much slower? Thanks! – IneedToAskQuestions Mar 22 '19 at 00:56
  • In your desired output, there is no comma between the toplevel json objects. Do you really want concatenated Json, or is this just a mistake in your question? –  Mar 22 '19 at 01:02
  • If you really want to produce concatenated Json, just serialize each object individually into a string (not using indented/formatted Json), and write the string as a line into your target file / target writer. "_Would adding a foreach loop make it much slower?_" Why would you think a foreach loop would make things much slower? What would be the big difference between the serializer iterating over the list and serializing each object in the list, compared to your code iterating over the the list and letting the serializer serialize each object? –  Mar 22 '19 at 01:05
  • You tagged this [tag:json.net] so are you looking for something like [How to apply indenting serialization only to some properties?](https://stackoverflow.com/q/28655996/3744182), [Creating JSON without array indentation](https://stackoverflow.com/q/53223517/3744182) and/or [Newtonsoft inline formatting for subelement while serializing](https://stackoverflow.com/q/30831895/3744182)? – dbc Mar 22 '19 at 02:42

0 Answers0