SubtitleEdit/libse/SeJsonParser.cs

1305 lines
48 KiB
C#
Raw Normal View History

using System.Collections.Generic;
using System.Text;
namespace Nikse.SubtitleEdit.Core
{
public class SeJsonParser
{
public enum SeJsonState
2020-05-09 10:20:32 +02:00
{
Object,
Array,
Value,
}
2019-09-19 16:42:59 +02:00
public List<string> Errors { get; private set; } = new List<string>();
public class StateElement
{
public SeJsonState State { get; set; }
public string Name { get; set; }
2019-09-19 16:42:59 +02:00
public int Count { get; set; }
}
2019-09-19 16:42:59 +02:00
private readonly HashSet<char> _whiteSpace = new HashSet<char> { ' ', '\r', '\n', '\t' };
2019-11-15 11:00:31 +01:00
public List<string> GetAllTagsByNameAsStrings(string content, string name)
{
2019-09-19 16:42:59 +02:00
Errors = new List<string>();
var list = new List<string>();
int i = 0;
int max = content.Length;
2019-09-19 16:42:59 +02:00
var state = new Stack<StateElement>();
var objectName = string.Empty;
while (i < max)
{
var ch = content[i];
2019-09-19 16:42:59 +02:00
if (_whiteSpace.Contains(ch)) // ignore white space
{
i++;
}
else if (state.Count == 0) // root
{
if (ch == '{')
{
2019-09-19 16:42:59 +02:00
state.Push(new StateElement
{
Name = "Root",
State = SeJsonState.Object
2019-09-19 16:42:59 +02:00
});
i++;
}
else if (ch == '[')
{
2019-09-19 16:42:59 +02:00
state.Push(new StateElement
{
Name = "Root",
State = SeJsonState.Array
2019-09-19 16:42:59 +02:00
});
i++;
}
2019-09-19 16:42:59 +02:00
else
{
Errors.Add($"Unexpected char {ch} at position {i}");
2019-09-19 16:42:59 +02:00
return list;
}
}
2019-09-19 16:42:59 +02:00
else if (state.Peek().State == SeJsonState.Object) // after '{'
{
2019-09-19 16:42:59 +02:00
if (ch == '"')
{
2019-09-19 16:42:59 +02:00
i++;
int end = content.IndexOf('"', i);
objectName = content.Substring(i, end - i).Trim();
int colon = content.IndexOf(':', end);
if (colon < 0)
{
Errors.Add($"Fatal - expected char : after position {end}");
2019-09-19 16:42:59 +02:00
return list;
}
i += colon - i + 1;
state.Push(new StateElement
{
Name = objectName,
State = SeJsonState.Value
2019-09-19 16:42:59 +02:00
});
}
2019-09-19 16:42:59 +02:00
else if (ch == '}')
{
2019-09-19 16:42:59 +02:00
i++;
state.Pop();
}
2019-09-19 16:42:59 +02:00
else if (ch == ',') // next object
{
2019-09-19 16:42:59 +02:00
i++;
if (state.Peek().Count > 0)
{
state.Peek().Count++;
}
else
{
Errors.Add($"Unexpected char {ch} at position {i}");
2019-09-19 16:42:59 +02:00
return list;
}
}
2019-09-19 16:42:59 +02:00
else if (ch == ']') // next object
2019-09-17 10:17:07 +02:00
{
2019-09-19 16:42:59 +02:00
i++;
if (state.Peek().Count > 0)
{
state.Pop();
}
else
{
Errors.Add($"Unexpected char {ch} at position {i}");
2019-09-19 16:42:59 +02:00
return list;
}
2019-09-17 10:17:07 +02:00
}
2019-09-19 16:42:59 +02:00
else
{
Errors.Add($"Unexpected char {ch} at position {i}");
2019-09-19 16:42:59 +02:00
return list;
}
2019-09-19 16:42:59 +02:00
}
else if (state.Peek().State == SeJsonState.Value) // value - string/ number / object / array / true / false / null + "," + "}"
2019-09-19 16:42:59 +02:00
{
if (ch == '"') // string
{
i++;
var skip = true;
int end = 0;
var endSeek = i;
while (skip)
{
end = content.IndexOf('"', endSeek);
if (end < 0)
{
2019-09-19 16:42:59 +02:00
Errors.Add($"Fatal - expected char \" after position {endSeek}");
return list;
}
skip = content[end - 1] == '\\';
if (skip)
{
endSeek = end + 1;
}
if (endSeek >= max)
{
2019-09-19 16:42:59 +02:00
Errors.Add($"Fatal - expected end tag after position {endSeek}");
return list;
}
}
var objectValue = content.Substring(i, end - i).Trim();
if (objectName == name)
{
list.Add(objectValue);
}
2019-09-19 16:42:59 +02:00
i += end - i + 1;
state.Pop();
2019-09-19 16:42:59 +02:00
if (state.Count > 0)
{
state.Peek().Count++;
}
}
else if (ch == '}') // empty value
{
i++;
var value = state.Pop();
if (state.Count > 0)
{
if (value.State == SeJsonState.Value)
2019-09-19 16:42:59 +02:00
{
state.Pop();
}
else
{
state.Peek().Count++;
}
}
}
else if (ch == ',') // next object
{
i++;
state.Pop();
if (state.Count > 0 && state.Peek().Count > 0)
{
state.Peek().Count++;
}
else
{
Errors.Add($"Unexpected char {ch} at position {i}");
2019-09-19 16:42:59 +02:00
return list;
}
}
else if (ch == 'n' && max > i + 3 && content[i + 1] == 'u' && content[i + 2] == 'l' && content[i + 3] == 'l')
{
2019-09-19 16:42:59 +02:00
i += 4;
state.Pop();
if (state.Count > 0)
{
state.Peek().Count++;
}
if (objectName == name)
{
2019-09-19 16:42:59 +02:00
list.Add(null);
}
2019-09-19 16:42:59 +02:00
}
else if (ch == 't' && max > i + 3 && content[i + 1] == 'r' && content[i + 2] == 'u' && content[i + 3] == 'e')
{
2019-09-19 16:42:59 +02:00
i += 4;
state.Pop();
if (state.Count > 0)
{
state.Peek().Count++;
}
if (objectName == name)
{
list.Add("true");
}
}
else if (ch == 'f' && max > i + 4 && content[i + 1] == 'a' && content[i + 2] == 'l' && content[i + 3] == 's' && content[i + 4] == 'e')
{
2019-09-19 16:42:59 +02:00
i += 5;
state.Pop();
if (state.Count > 0)
{
state.Peek().Count++;
}
if (objectName == name)
{
list.Add("false");
}
}
2019-09-19 16:42:59 +02:00
else if ("+-0123456789".IndexOf(ch) >= 0)
{
var sb = new StringBuilder();
2019-09-19 16:42:59 +02:00
while ("+-0123456789.Ee".IndexOf(content[i]) >= 0 && i < max)
{
sb.Append(content[i]);
i++;
}
2019-09-19 16:42:59 +02:00
state.Pop();
if (state.Count > 0)
{
state.Peek().Count++;
}
if (objectName == name)
{
list.Add(sb.ToString());
}
}
2019-09-19 16:42:59 +02:00
else if (ch == '{')
{
2019-09-19 16:42:59 +02:00
if (state.Count > 1)
{
2019-09-19 16:42:59 +02:00
var value = state.Pop();
state.Peek().Count++;
state.Push(value);
}
2019-09-19 16:42:59 +02:00
state.Push(new StateElement
{
State = SeJsonState.Object,
2019-09-19 16:42:59 +02:00
Name = objectName
});
i++;
}
else if (ch == '[')
{
if (state.Count > 1)
{
var value = state.Pop();
state.Peek().Count++;
state.Push(value);
}
2019-09-19 16:42:59 +02:00
state.Push(new StateElement
{
State = SeJsonState.Array,
2019-09-19 16:42:59 +02:00
Name = objectName
});
i++;
}
else
{
Errors.Add($"Unexpected char {ch} at position {i}");
2019-09-19 16:42:59 +02:00
return list;
}
}
2019-09-19 16:42:59 +02:00
else if (state.Peek().State == SeJsonState.Array) // array, after '['
{
2019-09-19 16:42:59 +02:00
if (ch == ']')
{
2019-09-19 16:42:59 +02:00
state.Pop();
i++;
}
2019-09-19 16:42:59 +02:00
else if (ch == ',' && state.Peek().Count > 0)
{
2019-09-19 16:42:59 +02:00
if (state.Count > 0 && state.Peek().Count > 0)
{
state.Peek().Count++;
}
else
{
Errors.Add($"Unexpected char {ch} at position {i}");
2019-09-19 16:42:59 +02:00
return list;
}
i++;
}
2019-09-19 16:42:59 +02:00
else if (ch == '{')
{
2019-09-19 16:42:59 +02:00
if (state.Count > 0)
{
2019-09-19 16:42:59 +02:00
state.Peek().Count++;
}
2019-09-19 16:42:59 +02:00
state.Push(new StateElement
{
Name = objectName,
State = SeJsonState.Object
2019-09-19 16:42:59 +02:00
});
i++;
}
else
{
if (state.Count > 0)
{
state.Peek().Count++;
}
state.Push(new StateElement
{
Name = objectName + "_array_value",
State = SeJsonState.Value
});
}
}
2019-09-19 16:42:59 +02:00
}
return list;
}
public List<string> GetArrayElementsByName(string content, string name)
{
2019-09-19 16:42:59 +02:00
Errors = new List<string>();
var list = new List<string>();
int i = 0;
int max = content.Length;
var state = new Stack<StateElement>();
var objectName = string.Empty;
2019-09-19 16:42:59 +02:00
var start = -1;
while (i < max)
{
var ch = content[i];
2019-09-19 16:42:59 +02:00
if (_whiteSpace.Contains(ch)) // ignore white space
{
i++;
}
2019-09-19 16:42:59 +02:00
else if (state.Count == 0) // root
{
2019-09-19 16:42:59 +02:00
if (ch == '{')
{
2019-09-19 16:42:59 +02:00
state.Push(new StateElement
{
2019-09-19 16:42:59 +02:00
Name = "Root",
State = SeJsonState.Object
2019-09-19 16:42:59 +02:00
});
i++;
}
2019-09-19 16:42:59 +02:00
else if (ch == '[')
{
2019-09-19 16:42:59 +02:00
state.Push(new StateElement
{
2019-09-19 16:42:59 +02:00
Name = "Root",
State = SeJsonState.Array
2019-09-19 16:42:59 +02:00
});
i++;
}
2019-09-19 16:42:59 +02:00
else
{
Errors.Add($"Unexpected char {ch} at position {i}");
2019-09-19 16:42:59 +02:00
return list;
}
}
2019-09-19 16:42:59 +02:00
else if (state.Peek().State == SeJsonState.Object) // after '{'
{
2019-09-19 16:42:59 +02:00
if (ch == '"')
{
2019-09-19 16:42:59 +02:00
i++;
int end = content.IndexOf('"', i);
objectName = content.Substring(i, end - i).Trim();
int colon = content.IndexOf(':', end);
if (colon < 0)
{
Errors.Add($"Fatal - expected char : after position {end}");
2019-09-19 16:42:59 +02:00
return list;
}
i += colon - i + 1;
state.Push(new StateElement
{
Name = objectName,
State = SeJsonState.Value
2019-09-19 16:42:59 +02:00
});
}
2019-09-17 10:17:07 +02:00
else if (ch == '}')
{
2019-09-19 16:42:59 +02:00
i++;
2019-09-17 10:17:07 +02:00
state.Pop();
}
2019-09-19 16:42:59 +02:00
else if (ch == ',') // next object
{
2019-09-19 16:42:59 +02:00
i++;
if (state.Peek().Count > 0)
{
state.Peek().Count++;
}
else
{
Errors.Add($"Unexpected char {ch} at position {i}");
2019-09-19 16:42:59 +02:00
return list;
}
}
2019-09-19 16:42:59 +02:00
else if (ch == ']') // next object
{
2019-09-19 16:42:59 +02:00
i++;
if (state.Peek().Count > 0)
{
2019-09-19 16:42:59 +02:00
state.Pop();
}
else
{
Errors.Add($"Unexpected char {ch} at position {i}");
2019-09-19 16:42:59 +02:00
return list;
}
}
2019-09-19 16:42:59 +02:00
else
{
Errors.Add($"Unexpected char {ch} at position {i}");
2019-09-19 16:42:59 +02:00
return list;
}
2019-09-19 16:42:59 +02:00
}
else if (state.Peek().State == SeJsonState.Value) // value - string/ number / object / array / true / false / null + "," + "}"
2019-09-19 16:42:59 +02:00
{
if (ch == '"') // string
{
i++;
var skip = true;
int end = 0;
2019-09-19 16:42:59 +02:00
var endSeek = i;
while (skip)
{
2019-09-19 16:42:59 +02:00
end = content.IndexOf('"', endSeek);
if (end < 0)
{
2019-09-19 16:42:59 +02:00
Errors.Add($"Fatal - expected char \" after position {endSeek}");
return list;
}
2019-09-19 16:42:59 +02:00
skip = content[end - 1] == '\\';
if (skip)
{
2019-09-19 16:42:59 +02:00
endSeek = end + 1;
}
2019-09-19 16:42:59 +02:00
if (endSeek >= max)
{
2019-09-19 16:42:59 +02:00
Errors.Add($"Fatal - expected end tag after position {endSeek}");
return list;
}
}
2019-09-19 16:42:59 +02:00
var objectValue = content.Substring(i, end - i).Trim();
if (objectName == name)
{
list.Add(objectValue);
start = -1;
2019-09-19 16:42:59 +02:00
}
i += end - i + 1;
state.Pop();
2019-09-19 16:42:59 +02:00
if (state.Count > 0)
{
state.Peek().Count++;
}
}
else if (ch == '}') // empty value
{
i++;
var value = state.Pop();
if (state.Count > 0)
{
if (value.State == SeJsonState.Value)
2019-09-19 16:42:59 +02:00
{
state.Pop();
}
else
{
state.Peek().Count++;
}
}
}
else if (ch == ',') // next object
{
i++;
state.Pop();
if (state.Count > 0 && state.Peek().Count > 0)
{
state.Peek().Count++;
}
else
{
Errors.Add($"Unexpected char {ch} at position {i}");
2019-09-19 16:42:59 +02:00
return list;
}
}
else if (ch == 'n' && max > i + 3 && content[i + 1] == 'u' && content[i + 2] == 'l' && content[i + 3] == 'l')
{
2019-09-19 16:42:59 +02:00
i += 4;
state.Pop();
2019-09-19 16:42:59 +02:00
if (state.Count > 0)
{
state.Peek().Count++;
}
if (objectName == name)
{
list.Add(null);
}
}
else if (ch == 't' && max > i + 3 && content[i + 1] == 'r' && content[i + 2] == 'u' && content[i + 3] == 'e')
{
2019-09-19 16:42:59 +02:00
i += 4;
state.Pop();
2019-09-19 16:42:59 +02:00
if (state.Count > 0)
{
state.Peek().Count++;
}
if (objectName == name)
{
list.Add("true");
}
}
else if (ch == 'f' && max > i + 4 && content[i + 1] == 'a' && content[i + 2] == 'l' && content[i + 3] == 's' && content[i + 4] == 'e')
{
2019-09-19 16:42:59 +02:00
i += 5;
state.Pop();
2019-09-19 16:42:59 +02:00
if (state.Count > 0)
{
state.Peek().Count++;
}
if (objectName == name)
{
list.Add("false");
}
}
2019-09-19 16:42:59 +02:00
else if ("+-0123456789".IndexOf(ch) >= 0)
{
2019-09-19 16:42:59 +02:00
var sb = new StringBuilder();
while (i < max && "+-0123456789.Ee".IndexOf(content[i]) >= 0)
{
2019-09-19 16:42:59 +02:00
sb.Append(content[i]);
i++;
}
state.Pop();
2019-09-19 16:42:59 +02:00
if (state.Count > 0)
{
state.Peek().Count++;
}
if (objectName == name)
{
list.Add(sb.ToString());
}
}
2019-09-19 16:42:59 +02:00
else if (ch == '{')
{
2019-09-19 16:42:59 +02:00
if (state.Count > 1)
{
2019-09-19 16:42:59 +02:00
var value = state.Pop();
state.Peek().Count++;
state.Push(value);
}
2019-09-19 16:42:59 +02:00
state.Push(new StateElement
{
State = SeJsonState.Object,
2019-09-19 16:42:59 +02:00
Name = objectName
});
i++;
}
else if (ch == '[')
{
if (state.Count > 1)
{
var value = state.Pop();
state.Peek().Count++;
state.Push(value);
}
2019-09-19 16:42:59 +02:00
state.Push(new StateElement
{
State = SeJsonState.Array,
2019-09-19 16:42:59 +02:00
Name = objectName
});
i++;
if (start < 0 && objectName == name)
{
start = i;
}
}
else
{
Errors.Add($"Unexpected char {ch} at position {i}");
2019-09-19 16:42:59 +02:00
return list;
}
}
2019-09-19 16:42:59 +02:00
else if (state.Peek().State == SeJsonState.Array) // array, after '['
{
2019-09-19 16:42:59 +02:00
if (ch == ']')
{
2019-09-19 16:42:59 +02:00
state.Pop();
i++;
if (state.Count > 0 && state.Peek().Name == name && start > -1)
{
list.Add(content.Substring(start, i - start - 1).Trim());
2019-09-19 16:42:59 +02:00
start = -1;
}
}
2019-09-19 16:42:59 +02:00
else if (ch == ',' && state.Peek().Count > 0)
{
if (start >= 0 && state.Peek().State == SeJsonState.Array && state.Peek().Name == name)
{
list.Add(content.Substring(start, i - start).Trim());
2019-09-19 16:42:59 +02:00
start = i + 1;
}
if (state.Count > 0 && state.Peek().Count > 0)
{
state.Peek().Count++;
}
else
{
Errors.Add($"Unexpected char {ch} at position {i}");
2019-09-19 16:42:59 +02:00
return list;
}
i++;
}
2019-09-19 16:42:59 +02:00
else if (ch == '{')
{
2019-09-19 16:42:59 +02:00
if (state.Count > 0)
{
2019-09-19 16:42:59 +02:00
state.Peek().Count++;
}
2019-09-19 16:42:59 +02:00
state.Push(new StateElement
{
Name = objectName,
State = SeJsonState.Object
2019-09-19 16:42:59 +02:00
});
i++;
}
else
{
if (state.Count > 0)
{
state.Peek().Count++;
}
state.Push(new StateElement
{
Name = objectName + "_array_value",
State = SeJsonState.Value
});
}
}
2019-09-19 16:42:59 +02:00
}
return list;
}
2019-09-19 16:42:59 +02:00
public List<string> GetArrayElements(string content)
{
Errors = new List<string>();
var list = new List<string>();
int i = 0;
int max = content.Length;
var state = new Stack<StateElement>();
var objectName = string.Empty;
var start = -1;
while (i < max)
{
var ch = content[i];
if (_whiteSpace.Contains(ch)) // ignore white space
{
i++;
}
else if (state.Count == 0) // root
{
if (ch == '{')
{
state.Push(new StateElement
{
Name = "Root",
State = SeJsonState.Object
});
i++;
}
else if (ch == '[')
{
state.Push(new StateElement
{
Name = "Root",
State = SeJsonState.Array
});
i++;
start = i;
}
else
{
Errors.Add($"Unexpected char {ch} at position {i}");
return list;
}
}
else if (state.Peek().State == SeJsonState.Object) // after '{'
{
if (ch == '"')
{
i++;
int end = content.IndexOf('"', i);
objectName = content.Substring(i, end - i).Trim();
int colon = content.IndexOf(':', end);
if (colon < 0)
{
Errors.Add($"Fatal - expected char : after position {end}");
return list;
}
i += colon - i + 1;
state.Push(new StateElement
{
Name = objectName,
State = SeJsonState.Value
});
}
else if (ch == '}')
{
i++;
state.Pop();
}
else if (ch == ',') // next object
{
i++;
if (state.Peek().Count > 0)
{
state.Peek().Count++;
}
else
{
Errors.Add($"Unexpected char {ch} at position {i}");
return list;
}
}
else if (ch == ']') // next object
{
i++;
if (state.Peek().Count > 0)
{
state.Pop();
}
else
{
Errors.Add($"Unexpected char {ch} at position {i}");
return list;
}
}
else
{
Errors.Add($"Unexpected char {ch} at position {i}");
return list;
}
}
else if (state.Peek().State == SeJsonState.Value) // value - string/ number / object / array / true / false / null + "," + "}"
{
if (ch == '"') // string
{
i++;
var skip = true;
int end = 0;
var endSeek = i;
while (skip)
{
end = content.IndexOf('"', endSeek);
if (end < 0)
{
Errors.Add($"Fatal - expected char \" after position {endSeek}");
return list;
}
skip = content[end - 1] == '\\';
if (skip)
{
endSeek = end + 1;
}
if (endSeek >= max)
{
Errors.Add($"Fatal - expected end tag after position {endSeek}");
return list;
}
}
i += end - i + 1;
state.Pop();
if (state.Count > 0)
{
state.Peek().Count++;
}
}
else if (ch == '}') // empty value
{
i++;
var value = state.Pop();
if (state.Count > 0)
{
if (value.State == SeJsonState.Value)
{
state.Pop();
}
else
{
state.Peek().Count++;
}
}
}
else if (ch == ',') // next object
{
i++;
state.Pop();
if (state.Count > 0 && state.Peek().Count > 0)
{
state.Peek().Count++;
}
else
{
Errors.Add($"Unexpected char {ch} at position {i}");
return list;
}
}
else if (ch == 'n' && max > i + 3 && content[i + 1] == 'u' && content[i + 2] == 'l' && content[i + 3] == 'l')
{
i += 4;
state.Pop();
if (state.Count > 0)
{
state.Peek().Count++;
}
}
else if (ch == 't' && max > i + 3 && content[i + 1] == 'r' && content[i + 2] == 'u' && content[i + 3] == 'e')
{
i += 4;
state.Pop();
if (state.Count > 0)
{
state.Peek().Count++;
}
}
else if (ch == 'f' && max > i + 4 && content[i + 1] == 'a' && content[i + 2] == 'l' && content[i + 3] == 's' && content[i + 4] == 'e')
{
i += 5;
state.Pop();
if (state.Count > 0)
{
state.Peek().Count++;
}
}
else if ("+-0123456789".IndexOf(ch) >= 0)
{
var sb = new StringBuilder();
while ("+-0123456789.Ee".IndexOf(content[i]) >= 0 && i < max)
{
sb.Append(content[i]);
i++;
}
state.Pop();
if (state.Count > 0)
{
state.Peek().Count++;
}
}
else if (ch == '{')
{
if (state.Count > 1)
{
var value = state.Pop();
state.Peek().Count++;
state.Push(value);
}
state.Push(new StateElement
{
State = SeJsonState.Object,
Name = objectName
});
i++;
}
else if (ch == '[')
{
if (state.Count > 1)
{
var value = state.Pop();
state.Peek().Count++;
state.Push(value);
}
state.Push(new StateElement
{
State = SeJsonState.Array,
Name = objectName
});
i++;
}
else
{
Errors.Add($"Unexpected char {ch} at position {i}");
return list;
}
}
2019-09-19 16:42:59 +02:00
else if (state.Peek().State == SeJsonState.Array) // array, after '['
{
if (ch == ']')
{
if (state.Count > 0 && state.Peek().Name == "Root" && start > -1)
{
list.Add(content.Substring(start, i - start));
start = -1;
}
state.Pop();
i++;
}
else if (ch == ',' && state.Peek().Count > 0)
{
if (start >= 0 && state.Count == 1 && state.Peek().State == SeJsonState.Array && state.Peek().Name == "Root")
{
list.Add(content.Substring(start, i - start));
start = i + 1;
}
if (state.Count > 0 && state.Peek().Count > 0)
{
state.Peek().Count++;
}
else
{
Errors.Add($"Unexpected char {ch} at position {i}");
return list;
}
i++;
}
else if (ch == '{')
{
if (state.Count > 0)
{
state.Peek().Count++;
}
state.Push(new StateElement
{
Name = objectName,
State = SeJsonState.Object
});
i++;
}
else
{
if (state.Count > 0)
{
state.Peek().Count++;
}
state.Push(new StateElement
{
Name = objectName + "_array_value",
State = SeJsonState.Value
});
}
}
}
return list;
}
2020-05-09 15:02:59 +02:00
/// <summary>
/// Get first object or value
/// </summary>
public string GetFirstObject(string content, string name)
{
Errors = new List<string>();
int i = 0;
int max = content.Length;
var state = new Stack<StateElement>();
var objectName = string.Empty;
var start = -1;
var startSateCount = -1;
while (i < max)
{
var ch = content[i];
if (_whiteSpace.Contains(ch)) // ignore white space
{
i++;
}
else if (state.Count == 0) // root
{
if (ch == '{')
{
state.Push(new StateElement
{
Name = "Root",
State = SeJsonState.Object
});
i++;
}
else if (ch == '[')
{
state.Push(new StateElement
{
Name = "Root",
State = SeJsonState.Array
});
i++;
}
else
{
Errors.Add($"Unexpected char {ch} at position {i}");
return string.Empty;
}
}
else if (state.Peek().State == SeJsonState.Object) // after '{'
{
if (ch == '"')
{
i++;
int end = content.IndexOf('"', i);
objectName = content.Substring(i, end - i).Trim();
int colon = content.IndexOf(':', end);
if (colon < 0)
{
Errors.Add($"Fatal - expected char : after position {end}");
return string.Empty;
}
i += colon - i + 1;
state.Push(new StateElement
{
Name = objectName,
State = SeJsonState.Value
});
if (objectName == name && start == -1)
{
start = i;
startSateCount = state.Count;
}
}
else if (ch == '}')
{
i++;
var s = state.Pop();
if (s.Name == name && state.Count == startSateCount && start >= 0)
{
return content.Substring(start, i - start);
}
}
else if (ch == ',') // next object
{
i++;
if (state.Peek().Count > 0)
{
state.Peek().Count++;
}
else
{
Errors.Add($"Unexpected char {ch} at position {i}");
return string.Empty;
}
}
else if (ch == ']') // next object
{
i++;
if (state.Peek().Count > 0)
{
state.Pop();
}
else
{
Errors.Add($"Unexpected char {ch} at position {i}");
return string.Empty;
}
}
else
{
Errors.Add($"Unexpected char {ch} at position {i}");
return string.Empty;
}
}
else if (state.Peek().State == SeJsonState.Value) // value - string/ number / object / array / true / false / null + "," + "}"
{
if (ch == '"') // string
{
i++;
var skip = true;
int end = 0;
var endSeek = i;
while (skip)
{
end = content.IndexOf('"', endSeek);
if (end < 0)
{
Errors.Add($"Fatal - expected char \" after position {endSeek}");
return string.Empty;
}
skip = content[end - 1] == '\\';
if (skip)
{
endSeek = end + 1;
}
if (endSeek >= max)
{
Errors.Add($"Fatal - expected end tag after position {endSeek}");
return string.Empty;
}
}
2020-05-09 15:02:59 +02:00
var objectValue = content.Substring(i, end - i).Trim();
if (objectName == name)
{
return objectValue;
}
i += end - i + 1;
state.Pop();
if (state.Count > 0)
{
state.Peek().Count++;
}
}
else if (ch == '}') // empty value
{
i++;
var value = state.Pop();
if (state.Count > 0)
{
if (value.State == SeJsonState.Value)
{
state.Pop();
}
else
{
state.Peek().Count++;
}
}
}
else if (ch == ',') // next object
{
i++;
state.Pop();
if (state.Count > 0 && state.Peek().Count > 0)
{
state.Peek().Count++;
}
else
{
Errors.Add($"Unexpected char {ch} at position {i}");
return string.Empty;
}
}
else if (ch == 'n' && max > i + 3 && content[i + 1] == 'u' && content[i + 2] == 'l' && content[i + 3] == 'l')
{
i += 4;
state.Pop();
if (state.Count > 0)
{
state.Peek().Count++;
}
}
else if (ch == 't' && max > i + 3 && content[i + 1] == 'r' && content[i + 2] == 'u' && content[i + 3] == 'e')
{
i += 4;
state.Pop();
if (state.Count > 0)
{
state.Peek().Count++;
}
}
else if (ch == 'f' && max > i + 4 && content[i + 1] == 'a' && content[i + 2] == 'l' && content[i + 3] == 's' && content[i + 4] == 'e')
{
i += 5;
state.Pop();
if (state.Count > 0)
{
state.Peek().Count++;
}
}
else if ("+-0123456789".IndexOf(ch) >= 0)
{
var sb = new StringBuilder();
while ("+-0123456789.Ee".IndexOf(content[i]) >= 0 && i < max)
{
sb.Append(content[i]);
i++;
}
2020-05-09 15:02:59 +02:00
if (objectName == name)
{
return sb.ToString();
}
state.Pop();
if (state.Count > 0)
{
state.Peek().Count++;
}
}
else if (ch == '{')
{
if (state.Count > 1)
{
var value = state.Pop();
state.Peek().Count++;
state.Push(value);
}
state.Push(new StateElement
{
State = SeJsonState.Object,
Name = objectName
});
i++;
}
else if (ch == '[')
{
if (state.Count > 1)
{
var value = state.Pop();
state.Peek().Count++;
state.Push(value);
}
state.Push(new StateElement
{
State = SeJsonState.Array,
Name = objectName
});
i++;
}
else
{
Errors.Add($"Unexpected char {ch} at position {i}");
return string.Empty;
}
}
else if (state.Peek().State == SeJsonState.Array) // array, after '['
{
if (ch == ']')
{
state.Pop();
i++;
}
else if (ch == ',' && state.Peek().Count > 0)
{
if (state.Count > 0 && state.Peek().Count > 0)
{
state.Peek().Count++;
}
else
{
Errors.Add($"Unexpected char {ch} at position {i}");
return string.Empty;
}
i++;
}
else if (ch == '{')
{
if (state.Count > 0)
{
state.Peek().Count++;
}
state.Push(new StateElement
{
Name = objectName,
State = SeJsonState.Object
});
i++;
}
else
{
if (state.Count > 0)
{
state.Peek().Count++;
}
state.Push(new StateElement
{
Name = objectName + "_array_value",
State = SeJsonState.Value
});
}
}
}
return string.Empty;
}
}
2019-11-15 11:00:31 +01:00
}