I managed to implement a solution. In a nutshell, when it encounteres { or < character, it uses Json or XML parser to parse it from that point onwards. If Json or XML parser throws an exception, I extract line and position on which exception occurred, then cut new part of processed string - from start position to one pointed to by the exception and try parsing again. If I fail, I simply accept { or < as a regular character and move on.
I guess it won't work in every single case and sometimes it may yield unexpected results (eg. JSON inside XML), but for my needs it's enough.
Uses ValueTuple. You'll have to implement your own containers for recognized parts as well (SimpleTextPart, XmlTextPart, JsonTextPart).
private int Move(string text, int current, int line, int position)
{
while (current < text.Length && (line > 1 || position > 1))
{
if (text[current] == '\n')
{
if (line == 1)
return -1;
line--;
}
else if (line == 1)
{
position--;
}
current++;
}
return current;
}
private (bool jsonParseResult, BaseTextPart part, int newIndex) TryParseJson(string text, int current)
{
try
{
string textPart = text.Substring(current);
JObject obj = JObject.Parse(textPart);
return (true, new JsonTextPart(obj), text.Length);
}
catch (JsonReaderException e)
{
int end = Move(text, current, e.LineNumber, e.LinePosition);
try
{
string textPart = text.Substring(current, end - current);
JObject obj = JObject.Parse(textPart);
return (true, new JsonTextPart(obj), end);
}
catch (JsonReaderException)
{
return (false, null, 0);
}
}
}
private (bool xmlParseResult, BaseTextPart part, int newIndex) TryParseXml(string text, int current)
{
XmlDocument doc = new XmlDocument();
try
{
string textPart = text.Substring(current);
doc.Load(new StringReader(textPart));
return (true, new XmlTextPart(doc), text.Length);
}
catch (XmlException e)
{
int end = Move(text, current, e.LineNumber, e.LinePosition);
try
{
string textPart = text.Substring(current, end - current);
doc.Load(new StringReader(textPart));
return (true, new XmlTextPart(doc), end);
}
catch (XmlException)
{
return (false, null, 0);
}
}
}
private List<BaseTextPart> Parse(string text)
{
var result = new List<BaseTextPart>();
int current = 0;
StringBuilder buffer = new StringBuilder();
while (current < text.Length)
{
if (text[current] == '{')
{
(bool jsonParseResult, BaseTextPart part, int newIndex) = TryParseJson(text, current);
if (jsonParseResult)
{
if (buffer.Length > 0)
{
result.Add(new SimpleTextPart(buffer.ToString()));
buffer.Clear();
}
result.Add(part);
current = newIndex;
continue;
}
}
if (text[current] == '<')
{
(bool xmlParseResult, BaseTextPart part, int newIndex) = TryParseXml(text, current);
if (xmlParseResult)
{
if (buffer.Length > 0)
{
result.Add(new SimpleTextPart(buffer.ToString()));
buffer.Clear();
}
result.Add(part);
current = newIndex;
continue;
}
}
buffer.Append(text[current]);
current++;
continue;
}
if (buffer.Length > 0)
{
result.Add(new SimpleTextPart(buffer.ToString()));
buffer.Clear();
}
return result;
}
JToken.ReadFrom()
(from Newtonsort.JSON library) will not throw exception you feed it json object followed by anything - it will only parse json object.Server response: {"field": "value"}
. I'd like to break it to parts, format automatically and visualize. Since my application is used to display logs in general, I have no way of knowing, what format log entries will have.