Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deserialize multiple json objects in one json string #2125

Closed
DimitriM opened this issue Aug 9, 2019 · 2 comments
Closed

Deserialize multiple json objects in one json string #2125

DimitriM opened this issue Aug 9, 2019 · 2 comments

Comments

@DimitriM
Copy link

DimitriM commented Aug 9, 2019

For deserialising multiple json objects in one json string I use the following function:

private IEnumerable<T> DeserializeObjects<T>(TextReader textReader, JsonSerializerSettings settings = null)
{
	var ser = JsonSerializer.CreateDefault(settings);
	var reader = new JsonTextReader(textReader);

	reader.SupportMultipleContent = true;

	while (reader.Read())
	{
		if (reader.TokenType == JsonToken.None || reader.TokenType == JsonToken.Undefined || reader.TokenType == JsonToken.Comment)
			continue;
		yield return ser.Deserialize<T>(reader);
	}
}

When I deserialize 2 complete json objects in one string it works perfectly:

{"dus":null,"due":null,"rtk":{"rva":false,"rac":true}}{"dus":null,"due":null,"rtk":{"rva":false,"rac":true}}

When the first object is not complete like:

{"rva":false,"rac":true}}{"dus":null,"due":null,"rtk":{"rva":false,"rac":true}}

The parser does something wrong and the CPU usage of the thread goes to 100%

@JamesNK
Copy link
Owner

JamesNK commented Aug 18, 2019

I couldn't reproduce this. With your code (deserializing into object) and JSON I got a reader error: JsonToken EndObject is not valid for closing JsonType None. Path '', line 1, position 25.

@dzmitry-lahoda
Copy link

and it seems split is natively supported https://www.newtonsoft.com/json/help/html/ReadMultipleContentWithJsonReader.htm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants