Refactoring and fixing a bug when add json text
This commit is contained in:
parent
aae12f70d3
commit
cab3ec2d06
@ -11,50 +11,47 @@
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
using System;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Specialized;
|
using System.Collections.Specialized;
|
||||||
|
|
||||||
|
|
||||||
namespace LitJson
|
namespace LitJson {
|
||||||
{
|
public enum JsonType {
|
||||||
public enum JsonType
|
None,
|
||||||
{
|
Object,
|
||||||
None,
|
Array,
|
||||||
|
String,
|
||||||
|
Int,
|
||||||
|
Long,
|
||||||
|
Double,
|
||||||
|
Boolean
|
||||||
|
}
|
||||||
|
|
||||||
Object,
|
public interface IJsonWrapper : IList, IOrderedDictionary {
|
||||||
Array,
|
Boolean IsArray { get; }
|
||||||
String,
|
Boolean IsBoolean { get; }
|
||||||
Int,
|
Boolean IsDouble { get; }
|
||||||
Long,
|
Boolean IsInt { get; }
|
||||||
Double,
|
Boolean IsLong { get; }
|
||||||
Boolean
|
Boolean IsObject { get; }
|
||||||
}
|
Boolean IsString { get; }
|
||||||
|
|
||||||
public interface IJsonWrapper : IList, IOrderedDictionary
|
Boolean GetBoolean();
|
||||||
{
|
Double GetDouble();
|
||||||
bool IsArray { get; }
|
Int32 GetInt();
|
||||||
bool IsBoolean { get; }
|
JsonType GetJsonType();
|
||||||
bool IsDouble { get; }
|
Int64 GetLong();
|
||||||
bool IsInt { get; }
|
String GetString();
|
||||||
bool IsLong { get; }
|
|
||||||
bool IsObject { get; }
|
|
||||||
bool IsString { get; }
|
|
||||||
|
|
||||||
bool GetBoolean ();
|
void SetBoolean(Boolean val);
|
||||||
double GetDouble ();
|
void SetDouble(Double val);
|
||||||
int GetInt ();
|
void SetInt(Int32 val);
|
||||||
JsonType GetJsonType ();
|
void SetJsonType(JsonType type);
|
||||||
long GetLong ();
|
void SetLong(Int64 val);
|
||||||
string GetString ();
|
void SetString(String val);
|
||||||
|
|
||||||
void SetBoolean (bool val);
|
String ToJson();
|
||||||
void SetDouble (double val);
|
void ToJson(JsonWriter writer);
|
||||||
void SetInt (int val);
|
}
|
||||||
void SetJsonType (JsonType type);
|
|
||||||
void SetLong (long val);
|
|
||||||
void SetString (string val);
|
|
||||||
|
|
||||||
string ToJson ();
|
|
||||||
void ToJson (JsonWriter writer);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -12,54 +12,26 @@
|
|||||||
using System;
|
using System;
|
||||||
|
|
||||||
|
|
||||||
namespace LitJson
|
namespace LitJson {
|
||||||
{
|
public class JsonException :
|
||||||
public class JsonException :
|
|
||||||
#if NETSTANDARD1_5
|
#if NETSTANDARD1_5
|
||||||
Exception
|
Exception
|
||||||
#else
|
#else
|
||||||
ApplicationException
|
ApplicationException
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
public JsonException () : base ()
|
public JsonException() : base() { }
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
internal JsonException (ParserToken token) :
|
internal JsonException(ParserToken token) : base(String.Format("Invalid token '{0}' in input string", token)) { }
|
||||||
base (String.Format (
|
|
||||||
"Invalid token '{0}' in input string", token))
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
internal JsonException (ParserToken token,
|
internal JsonException(ParserToken token, Exception inner_exception) : base(String.Format("Invalid token '{0}' in input string", token), inner_exception) { }
|
||||||
Exception inner_exception) :
|
|
||||||
base (String.Format (
|
|
||||||
"Invalid token '{0}' in input string", token),
|
|
||||||
inner_exception)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
internal JsonException (int c) :
|
internal JsonException(Int32 c) : base(String.Format("Invalid character '{0}' in input string", (Char)c)) { }
|
||||||
base (String.Format (
|
|
||||||
"Invalid character '{0}' in input string", (char) c))
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
internal JsonException (int c, Exception inner_exception) :
|
internal JsonException(Int32 c, Exception inner_exception) : base(String.Format("Invalid character '{0}' in input string", (Char)c), inner_exception) { }
|
||||||
base (String.Format (
|
|
||||||
"Invalid character '{0}' in input string", (char) c),
|
|
||||||
inner_exception)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
|
public JsonException(String message) : base(message) { }
|
||||||
|
|
||||||
public JsonException (string message) : base (message)
|
public JsonException(String message, Exception inner_exception) : base(message, inner_exception) { }
|
||||||
{
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public JsonException (string message, Exception inner_exception) :
|
|
||||||
base (message, inner_exception)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -436,7 +436,7 @@ namespace LitJson {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static IJsonWrapper ReadValue(WrapperFactory factory, JsonReader reader) {
|
private static IJsonWrapper ReadValue(WrapperFactory factory, JsonReader reader) {
|
||||||
reader.Read();
|
_ = reader.Read();
|
||||||
|
|
||||||
if (reader.Token == JsonToken.ArrayEnd ||
|
if (reader.Token == JsonToken.ArrayEnd ||
|
||||||
reader.Token == JsonToken.Null) {
|
reader.Token == JsonToken.Null) {
|
||||||
@ -478,13 +478,13 @@ namespace LitJson {
|
|||||||
if (item == null && reader.Token == JsonToken.ArrayEnd) {
|
if (item == null && reader.Token == JsonToken.ArrayEnd) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
instance.Add(item);
|
_ = instance.Add(item);
|
||||||
}
|
}
|
||||||
} else if (reader.Token == JsonToken.ObjectStart) {
|
} else if (reader.Token == JsonToken.ObjectStart) {
|
||||||
instance.SetJsonType(JsonType.Object);
|
instance.SetJsonType(JsonType.Object);
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
reader.Read();
|
_ = reader.Read();
|
||||||
|
|
||||||
if (reader.Token == JsonToken.ObjectEnd) {
|
if (reader.Token == JsonToken.ObjectEnd) {
|
||||||
break;
|
break;
|
||||||
@ -627,7 +627,10 @@ namespace LitJson {
|
|||||||
|
|
||||||
if (obj is IJsonWrapper) {
|
if (obj is IJsonWrapper) {
|
||||||
if (writer_is_private) {
|
if (writer_is_private) {
|
||||||
writer.TextWriter.Write(((IJsonWrapper)obj).ToJson());
|
String t = ((IJsonWrapper)obj).ToJson();
|
||||||
|
writer.WriteJson(t);
|
||||||
|
//writer.TextWriter.Write(t);
|
||||||
|
//this.context.ExpectingValue = false;
|
||||||
} else {
|
} else {
|
||||||
((IJsonWrapper)obj).ToJson(writer);
|
((IJsonWrapper)obj).ToJson(writer);
|
||||||
}
|
}
|
||||||
@ -783,9 +786,9 @@ namespace LitJson {
|
|||||||
|
|
||||||
public static IJsonWrapper ToWrapper(WrapperFactory factory, String json) => ReadValue(factory, new JsonReader(json));
|
public static IJsonWrapper ToWrapper(WrapperFactory factory, String json) => ReadValue(factory, new JsonReader(json));
|
||||||
|
|
||||||
public static void RegisterExporter<T>(ExporterFunc<T> exporter) => custom_exporters_table[typeof(T)] = (Object obj, JsonWriter writer) => { exporter((T)obj, writer); };
|
public static void RegisterExporter<T>(ExporterFunc<T> exporter) => custom_exporters_table[typeof(T)] = (Object obj, JsonWriter writer) => exporter((T)obj, writer);
|
||||||
|
|
||||||
public static void RegisterImporter<TJson, TValue>(ImporterFunc<TJson, TValue> importer) => RegisterImporter(custom_importers_table, typeof(TJson), typeof(TValue), (Object input) => { return importer((TJson)input); });
|
public static void RegisterImporter<TJson, TValue>(ImporterFunc<TJson, TValue> importer) => RegisterImporter(custom_importers_table, typeof(TJson), typeof(TValue), (Object input) => importer((TJson)input));
|
||||||
|
|
||||||
public static void UnregisterExporters() => custom_exporters_table.Clear();
|
public static void UnregisterExporters() => custom_exporters_table.Clear();
|
||||||
|
|
||||||
|
@ -15,91 +15,118 @@ using System.Collections;
|
|||||||
using System.Collections.Specialized;
|
using System.Collections.Specialized;
|
||||||
|
|
||||||
|
|
||||||
namespace LitJson
|
namespace LitJson {
|
||||||
{
|
public class JsonMockWrapper : IJsonWrapper {
|
||||||
public class JsonMockWrapper : IJsonWrapper
|
public Boolean IsArray => false;
|
||||||
{
|
|
||||||
public bool IsArray { get { return false; } }
|
|
||||||
public bool IsBoolean { get { return false; } }
|
|
||||||
public bool IsDouble { get { return false; } }
|
|
||||||
public bool IsInt { get { return false; } }
|
|
||||||
public bool IsLong { get { return false; } }
|
|
||||||
public bool IsObject { get { return false; } }
|
|
||||||
public bool IsString { get { return false; } }
|
|
||||||
|
|
||||||
public bool GetBoolean () { return false; }
|
public Boolean IsBoolean => false;
|
||||||
public double GetDouble () { return 0.0; }
|
|
||||||
public int GetInt () { return 0; }
|
|
||||||
public JsonType GetJsonType () { return JsonType.None; }
|
|
||||||
public long GetLong () { return 0L; }
|
|
||||||
public string GetString () { return ""; }
|
|
||||||
|
|
||||||
public void SetBoolean (bool val) {}
|
public Boolean IsDouble => false;
|
||||||
public void SetDouble (double val) {}
|
|
||||||
public void SetInt (int val) {}
|
|
||||||
public void SetJsonType (JsonType type) {}
|
|
||||||
public void SetLong (long val) {}
|
|
||||||
public void SetString (string val) {}
|
|
||||||
|
|
||||||
public string ToJson () { return ""; }
|
public Boolean IsInt => false;
|
||||||
public void ToJson (JsonWriter writer) {}
|
|
||||||
|
|
||||||
|
public Boolean IsLong => false;
|
||||||
|
|
||||||
bool IList.IsFixedSize { get { return true; } }
|
public Boolean IsObject => false;
|
||||||
bool IList.IsReadOnly { get { return true; } }
|
|
||||||
|
|
||||||
object IList.this[int index] {
|
public Boolean IsString => false;
|
||||||
get { return null; }
|
|
||||||
set {}
|
|
||||||
}
|
|
||||||
|
|
||||||
int IList.Add (object value) { return 0; }
|
public Boolean GetBoolean() => false;
|
||||||
void IList.Clear () {}
|
|
||||||
bool IList.Contains (object value) { return false; }
|
|
||||||
int IList.IndexOf (object value) { return -1; }
|
|
||||||
void IList.Insert (int i, object v) {}
|
|
||||||
void IList.Remove (object value) {}
|
|
||||||
void IList.RemoveAt (int index) {}
|
|
||||||
|
|
||||||
|
public Double GetDouble() => 0.0;
|
||||||
|
|
||||||
int ICollection.Count { get { return 0; } }
|
public Int32 GetInt() => 0;
|
||||||
bool ICollection.IsSynchronized { get { return false; } }
|
|
||||||
object ICollection.SyncRoot { get { return null; } }
|
|
||||||
|
|
||||||
void ICollection.CopyTo (Array array, int index) {}
|
public JsonType GetJsonType() => JsonType.None;
|
||||||
|
|
||||||
|
public Int64 GetLong() => 0L;
|
||||||
|
|
||||||
IEnumerator IEnumerable.GetEnumerator () { return null; }
|
public String GetString() => "";
|
||||||
|
|
||||||
|
public void SetBoolean(Boolean val) { }
|
||||||
|
|
||||||
bool IDictionary.IsFixedSize { get { return true; } }
|
public void SetDouble(Double val) { }
|
||||||
bool IDictionary.IsReadOnly { get { return true; } }
|
|
||||||
|
|
||||||
ICollection IDictionary.Keys { get { return null; } }
|
public void SetInt(Int32 val) { }
|
||||||
ICollection IDictionary.Values { get { return null; } }
|
|
||||||
|
|
||||||
object IDictionary.this[object key] {
|
public void SetJsonType(JsonType type) { }
|
||||||
get { return null; }
|
|
||||||
set {}
|
|
||||||
}
|
|
||||||
|
|
||||||
void IDictionary.Add (object k, object v) {}
|
public void SetLong(Int64 val) { }
|
||||||
void IDictionary.Clear () {}
|
|
||||||
bool IDictionary.Contains (object key) { return false; }
|
|
||||||
void IDictionary.Remove (object key) {}
|
|
||||||
|
|
||||||
IDictionaryEnumerator IDictionary.GetEnumerator () { return null; }
|
public void SetString(String val) { }
|
||||||
|
|
||||||
|
public String ToJson() => "";
|
||||||
|
|
||||||
object IOrderedDictionary.this[int idx] {
|
public void ToJson(JsonWriter writer) { }
|
||||||
get { return null; }
|
|
||||||
set {}
|
|
||||||
}
|
|
||||||
|
|
||||||
IDictionaryEnumerator IOrderedDictionary.GetEnumerator () {
|
Boolean IList.IsFixedSize => true;
|
||||||
return null;
|
|
||||||
}
|
Boolean IList.IsReadOnly => true;
|
||||||
void IOrderedDictionary.Insert (int i, object k, object v) {}
|
|
||||||
void IOrderedDictionary.RemoveAt (int i) {}
|
Object IList.this[Int32 index] {
|
||||||
|
get => null;
|
||||||
|
set {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Int32 IList.Add(Object value) => 0;
|
||||||
|
|
||||||
|
void IList.Clear() { }
|
||||||
|
|
||||||
|
Boolean IList.Contains(Object value) => false;
|
||||||
|
|
||||||
|
Int32 IList.IndexOf(Object value) => -1;
|
||||||
|
|
||||||
|
void IList.Insert(Int32 i, Object v) { }
|
||||||
|
|
||||||
|
void IList.Remove(Object value) { }
|
||||||
|
|
||||||
|
void IList.RemoveAt(Int32 index) { }
|
||||||
|
|
||||||
|
Int32 ICollection.Count => 0;
|
||||||
|
|
||||||
|
Boolean ICollection.IsSynchronized => false;
|
||||||
|
|
||||||
|
Object ICollection.SyncRoot => null;
|
||||||
|
|
||||||
|
void ICollection.CopyTo(Array array, Int32 index) { }
|
||||||
|
|
||||||
|
IEnumerator IEnumerable.GetEnumerator() => null;
|
||||||
|
|
||||||
|
Boolean IDictionary.IsFixedSize => true;
|
||||||
|
|
||||||
|
Boolean IDictionary.IsReadOnly => true;
|
||||||
|
|
||||||
|
ICollection IDictionary.Keys => null;
|
||||||
|
|
||||||
|
ICollection IDictionary.Values => null;
|
||||||
|
|
||||||
|
Object IDictionary.this[Object key] {
|
||||||
|
get => null;
|
||||||
|
set {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void IDictionary.Add(Object k, Object v) { }
|
||||||
|
|
||||||
|
void IDictionary.Clear() { }
|
||||||
|
|
||||||
|
Boolean IDictionary.Contains(Object key) => false;
|
||||||
|
|
||||||
|
void IDictionary.Remove(Object key) { }
|
||||||
|
|
||||||
|
IDictionaryEnumerator IDictionary.GetEnumerator() => null;
|
||||||
|
|
||||||
|
Object IOrderedDictionary.this[Int32 idx] {
|
||||||
|
get => null;
|
||||||
|
set {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
IDictionaryEnumerator IOrderedDictionary.GetEnumerator() => null;
|
||||||
|
|
||||||
|
void IOrderedDictionary.Insert(Int32 i, Object k, Object v) { }
|
||||||
|
|
||||||
|
void IOrderedDictionary.RemoveAt(Int32 i) { }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,463 +16,353 @@ using System.IO;
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
|
|
||||||
namespace LitJson
|
namespace LitJson {
|
||||||
{
|
public enum JsonToken {
|
||||||
public enum JsonToken
|
None,
|
||||||
{
|
ObjectStart,
|
||||||
None,
|
PropertyName,
|
||||||
|
ObjectEnd,
|
||||||
|
ArrayStart,
|
||||||
|
ArrayEnd,
|
||||||
|
Int,
|
||||||
|
Long,
|
||||||
|
Double,
|
||||||
|
String,
|
||||||
|
Boolean,
|
||||||
|
Null
|
||||||
|
}
|
||||||
|
|
||||||
ObjectStart,
|
|
||||||
PropertyName,
|
|
||||||
ObjectEnd,
|
|
||||||
|
|
||||||
ArrayStart,
|
public class JsonReader {
|
||||||
ArrayEnd,
|
#region Fields
|
||||||
|
private static readonly IDictionary<Int32, IDictionary<Int32, Int32[]>> parse_table;
|
||||||
|
|
||||||
Int,
|
private readonly Stack<Int32> automaton_stack;
|
||||||
Long,
|
private Int32 current_input;
|
||||||
Double,
|
private Int32 current_symbol;
|
||||||
|
private readonly Lexer lexer;
|
||||||
|
private Boolean parser_in_string;
|
||||||
|
private Boolean parser_return;
|
||||||
|
private Boolean read_started;
|
||||||
|
private TextReader reader;
|
||||||
|
private readonly Boolean reader_is_owned;
|
||||||
|
#endregion
|
||||||
|
|
||||||
String,
|
#region Public Properties
|
||||||
|
public Boolean AllowComments {
|
||||||
Boolean,
|
get => this.lexer.AllowComments;
|
||||||
Null
|
set => this.lexer.AllowComments = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Boolean AllowSingleQuotedStrings {
|
||||||
public class JsonReader
|
get => this.lexer.AllowSingleQuotedStrings;
|
||||||
{
|
set => this.lexer.AllowSingleQuotedStrings = value;
|
||||||
#region Fields
|
|
||||||
private static readonly IDictionary<int, IDictionary<int, int[]>> parse_table;
|
|
||||||
|
|
||||||
private Stack<int> automaton_stack;
|
|
||||||
private int current_input;
|
|
||||||
private int current_symbol;
|
|
||||||
private bool end_of_json;
|
|
||||||
private bool end_of_input;
|
|
||||||
private Lexer lexer;
|
|
||||||
private bool parser_in_string;
|
|
||||||
private bool parser_return;
|
|
||||||
private bool read_started;
|
|
||||||
private TextReader reader;
|
|
||||||
private bool reader_is_owned;
|
|
||||||
private bool skip_non_members;
|
|
||||||
private object token_value;
|
|
||||||
private JsonToken token;
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
|
|
||||||
#region Public Properties
|
|
||||||
public bool AllowComments {
|
|
||||||
get { return lexer.AllowComments; }
|
|
||||||
set { lexer.AllowComments = value; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool AllowSingleQuotedStrings {
|
|
||||||
get { return lexer.AllowSingleQuotedStrings; }
|
|
||||||
set { lexer.AllowSingleQuotedStrings = value; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool SkipNonMembers {
|
|
||||||
get { return skip_non_members; }
|
|
||||||
set { skip_non_members = value; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool EndOfInput {
|
|
||||||
get { return end_of_input; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool EndOfJson {
|
|
||||||
get { return end_of_json; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public JsonToken Token {
|
|
||||||
get { return token; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public object Value {
|
|
||||||
get { return token_value; }
|
|
||||||
}
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
|
|
||||||
#region Constructors
|
|
||||||
static JsonReader ()
|
|
||||||
{
|
|
||||||
parse_table = PopulateParseTable ();
|
|
||||||
}
|
|
||||||
|
|
||||||
public JsonReader (string json_text) :
|
|
||||||
this (new StringReader (json_text), true)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public JsonReader (TextReader reader) :
|
|
||||||
this (reader, false)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
private JsonReader (TextReader reader, bool owned)
|
|
||||||
{
|
|
||||||
if (reader == null)
|
|
||||||
throw new ArgumentNullException ("reader");
|
|
||||||
|
|
||||||
parser_in_string = false;
|
|
||||||
parser_return = false;
|
|
||||||
|
|
||||||
read_started = false;
|
|
||||||
automaton_stack = new Stack<int> ();
|
|
||||||
automaton_stack.Push ((int) ParserToken.End);
|
|
||||||
automaton_stack.Push ((int) ParserToken.Text);
|
|
||||||
|
|
||||||
lexer = new Lexer (reader);
|
|
||||||
|
|
||||||
end_of_input = false;
|
|
||||||
end_of_json = false;
|
|
||||||
|
|
||||||
skip_non_members = true;
|
|
||||||
|
|
||||||
this.reader = reader;
|
|
||||||
reader_is_owned = owned;
|
|
||||||
}
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
|
|
||||||
#region Static Methods
|
|
||||||
private static IDictionary<int, IDictionary<int, int[]>> PopulateParseTable ()
|
|
||||||
{
|
|
||||||
// See section A.2. of the manual for details
|
|
||||||
IDictionary<int, IDictionary<int, int[]>> parse_table = new Dictionary<int, IDictionary<int, int[]>> ();
|
|
||||||
|
|
||||||
TableAddRow (parse_table, ParserToken.Array);
|
|
||||||
TableAddCol (parse_table, ParserToken.Array, '[',
|
|
||||||
'[',
|
|
||||||
(int) ParserToken.ArrayPrime);
|
|
||||||
|
|
||||||
TableAddRow (parse_table, ParserToken.ArrayPrime);
|
|
||||||
TableAddCol (parse_table, ParserToken.ArrayPrime, '"',
|
|
||||||
(int) ParserToken.Value,
|
|
||||||
|
|
||||||
(int) ParserToken.ValueRest,
|
|
||||||
']');
|
|
||||||
TableAddCol (parse_table, ParserToken.ArrayPrime, '[',
|
|
||||||
(int) ParserToken.Value,
|
|
||||||
(int) ParserToken.ValueRest,
|
|
||||||
']');
|
|
||||||
TableAddCol (parse_table, ParserToken.ArrayPrime, ']',
|
|
||||||
']');
|
|
||||||
TableAddCol (parse_table, ParserToken.ArrayPrime, '{',
|
|
||||||
(int) ParserToken.Value,
|
|
||||||
(int) ParserToken.ValueRest,
|
|
||||||
']');
|
|
||||||
TableAddCol (parse_table, ParserToken.ArrayPrime, (int) ParserToken.Number,
|
|
||||||
(int) ParserToken.Value,
|
|
||||||
(int) ParserToken.ValueRest,
|
|
||||||
']');
|
|
||||||
TableAddCol (parse_table, ParserToken.ArrayPrime, (int) ParserToken.True,
|
|
||||||
(int) ParserToken.Value,
|
|
||||||
(int) ParserToken.ValueRest,
|
|
||||||
']');
|
|
||||||
TableAddCol (parse_table, ParserToken.ArrayPrime, (int) ParserToken.False,
|
|
||||||
(int) ParserToken.Value,
|
|
||||||
(int) ParserToken.ValueRest,
|
|
||||||
']');
|
|
||||||
TableAddCol (parse_table, ParserToken.ArrayPrime, (int) ParserToken.Null,
|
|
||||||
(int) ParserToken.Value,
|
|
||||||
(int) ParserToken.ValueRest,
|
|
||||||
']');
|
|
||||||
|
|
||||||
TableAddRow (parse_table, ParserToken.Object);
|
|
||||||
TableAddCol (parse_table, ParserToken.Object, '{',
|
|
||||||
'{',
|
|
||||||
(int) ParserToken.ObjectPrime);
|
|
||||||
|
|
||||||
TableAddRow (parse_table, ParserToken.ObjectPrime);
|
|
||||||
TableAddCol (parse_table, ParserToken.ObjectPrime, '"',
|
|
||||||
(int) ParserToken.Pair,
|
|
||||||
(int) ParserToken.PairRest,
|
|
||||||
'}');
|
|
||||||
TableAddCol (parse_table, ParserToken.ObjectPrime, '}',
|
|
||||||
'}');
|
|
||||||
|
|
||||||
TableAddRow (parse_table, ParserToken.Pair);
|
|
||||||
TableAddCol (parse_table, ParserToken.Pair, '"',
|
|
||||||
(int) ParserToken.String,
|
|
||||||
':',
|
|
||||||
(int) ParserToken.Value);
|
|
||||||
|
|
||||||
TableAddRow (parse_table, ParserToken.PairRest);
|
|
||||||
TableAddCol (parse_table, ParserToken.PairRest, ',',
|
|
||||||
',',
|
|
||||||
(int) ParserToken.Pair,
|
|
||||||
(int) ParserToken.PairRest);
|
|
||||||
TableAddCol (parse_table, ParserToken.PairRest, '}',
|
|
||||||
(int) ParserToken.Epsilon);
|
|
||||||
|
|
||||||
TableAddRow (parse_table, ParserToken.String);
|
|
||||||
TableAddCol (parse_table, ParserToken.String, '"',
|
|
||||||
'"',
|
|
||||||
(int) ParserToken.CharSeq,
|
|
||||||
'"');
|
|
||||||
|
|
||||||
TableAddRow (parse_table, ParserToken.Text);
|
|
||||||
TableAddCol (parse_table, ParserToken.Text, '[',
|
|
||||||
(int) ParserToken.Array);
|
|
||||||
TableAddCol (parse_table, ParserToken.Text, '{',
|
|
||||||
(int) ParserToken.Object);
|
|
||||||
|
|
||||||
TableAddRow (parse_table, ParserToken.Value);
|
|
||||||
TableAddCol (parse_table, ParserToken.Value, '"',
|
|
||||||
(int) ParserToken.String);
|
|
||||||
TableAddCol (parse_table, ParserToken.Value, '[',
|
|
||||||
(int) ParserToken.Array);
|
|
||||||
TableAddCol (parse_table, ParserToken.Value, '{',
|
|
||||||
(int) ParserToken.Object);
|
|
||||||
TableAddCol (parse_table, ParserToken.Value, (int) ParserToken.Number,
|
|
||||||
(int) ParserToken.Number);
|
|
||||||
TableAddCol (parse_table, ParserToken.Value, (int) ParserToken.True,
|
|
||||||
(int) ParserToken.True);
|
|
||||||
TableAddCol (parse_table, ParserToken.Value, (int) ParserToken.False,
|
|
||||||
(int) ParserToken.False);
|
|
||||||
TableAddCol (parse_table, ParserToken.Value, (int) ParserToken.Null,
|
|
||||||
(int) ParserToken.Null);
|
|
||||||
|
|
||||||
TableAddRow (parse_table, ParserToken.ValueRest);
|
|
||||||
TableAddCol (parse_table, ParserToken.ValueRest, ',',
|
|
||||||
',',
|
|
||||||
(int) ParserToken.Value,
|
|
||||||
(int) ParserToken.ValueRest);
|
|
||||||
TableAddCol (parse_table, ParserToken.ValueRest, ']',
|
|
||||||
(int) ParserToken.Epsilon);
|
|
||||||
|
|
||||||
return parse_table;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void TableAddCol (IDictionary<int, IDictionary<int, int[]>> parse_table, ParserToken row, int col,
|
|
||||||
params int[] symbols)
|
|
||||||
{
|
|
||||||
parse_table[(int) row].Add (col, symbols);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void TableAddRow (IDictionary<int, IDictionary<int, int[]>> parse_table, ParserToken rule)
|
|
||||||
{
|
|
||||||
parse_table.Add ((int) rule, new Dictionary<int, int[]> ());
|
|
||||||
}
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
|
|
||||||
#region Private Methods
|
|
||||||
private void ProcessNumber (string number)
|
|
||||||
{
|
|
||||||
if (number.IndexOf ('.') != -1 ||
|
|
||||||
number.IndexOf ('e') != -1 ||
|
|
||||||
number.IndexOf ('E') != -1) {
|
|
||||||
|
|
||||||
double n_double;
|
|
||||||
if (double.TryParse (number, NumberStyles.Any, CultureInfo.InvariantCulture, out n_double)) {
|
|
||||||
token = JsonToken.Double;
|
|
||||||
token_value = n_double;
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int n_int32;
|
|
||||||
if (int.TryParse (number, NumberStyles.Integer, CultureInfo.InvariantCulture, out n_int32)) {
|
|
||||||
token = JsonToken.Int;
|
|
||||||
token_value = n_int32;
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
long n_int64;
|
|
||||||
if (long.TryParse (number, NumberStyles.Integer, CultureInfo.InvariantCulture, out n_int64)) {
|
|
||||||
token = JsonToken.Long;
|
|
||||||
token_value = n_int64;
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
ulong n_uint64;
|
|
||||||
if (ulong.TryParse(number, NumberStyles.Integer, CultureInfo.InvariantCulture, out n_uint64))
|
|
||||||
{
|
|
||||||
token = JsonToken.Long;
|
|
||||||
token_value = n_uint64;
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Shouldn't happen, but just in case, return something
|
|
||||||
token = JsonToken.Int;
|
|
||||||
token_value = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void ProcessSymbol ()
|
|
||||||
{
|
|
||||||
if (current_symbol == '[') {
|
|
||||||
token = JsonToken.ArrayStart;
|
|
||||||
parser_return = true;
|
|
||||||
|
|
||||||
} else if (current_symbol == ']') {
|
|
||||||
token = JsonToken.ArrayEnd;
|
|
||||||
parser_return = true;
|
|
||||||
|
|
||||||
} else if (current_symbol == '{') {
|
|
||||||
token = JsonToken.ObjectStart;
|
|
||||||
parser_return = true;
|
|
||||||
|
|
||||||
} else if (current_symbol == '}') {
|
|
||||||
token = JsonToken.ObjectEnd;
|
|
||||||
parser_return = true;
|
|
||||||
|
|
||||||
} else if (current_symbol == '"') {
|
|
||||||
if (parser_in_string) {
|
|
||||||
parser_in_string = false;
|
|
||||||
|
|
||||||
parser_return = true;
|
|
||||||
|
|
||||||
} else {
|
|
||||||
if (token == JsonToken.None)
|
|
||||||
token = JsonToken.String;
|
|
||||||
|
|
||||||
parser_in_string = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
} else if (current_symbol == (int) ParserToken.CharSeq) {
|
|
||||||
token_value = lexer.StringValue;
|
|
||||||
|
|
||||||
} else if (current_symbol == (int) ParserToken.False) {
|
|
||||||
token = JsonToken.Boolean;
|
|
||||||
token_value = false;
|
|
||||||
parser_return = true;
|
|
||||||
|
|
||||||
} else if (current_symbol == (int) ParserToken.Null) {
|
|
||||||
token = JsonToken.Null;
|
|
||||||
parser_return = true;
|
|
||||||
|
|
||||||
} else if (current_symbol == (int) ParserToken.Number) {
|
|
||||||
ProcessNumber (lexer.StringValue);
|
|
||||||
|
|
||||||
parser_return = true;
|
|
||||||
|
|
||||||
} else if (current_symbol == (int) ParserToken.Pair) {
|
|
||||||
token = JsonToken.PropertyName;
|
|
||||||
|
|
||||||
} else if (current_symbol == (int) ParserToken.True) {
|
|
||||||
token = JsonToken.Boolean;
|
|
||||||
token_value = true;
|
|
||||||
parser_return = true;
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private bool ReadToken ()
|
|
||||||
{
|
|
||||||
if (end_of_input)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
lexer.NextToken ();
|
|
||||||
|
|
||||||
if (lexer.EndOfInput) {
|
|
||||||
Close ();
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
current_input = lexer.Token;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
|
|
||||||
public void Close ()
|
|
||||||
{
|
|
||||||
if (end_of_input)
|
|
||||||
return;
|
|
||||||
|
|
||||||
end_of_input = true;
|
|
||||||
end_of_json = true;
|
|
||||||
|
|
||||||
if (reader_is_owned)
|
|
||||||
{
|
|
||||||
using(reader){}
|
|
||||||
}
|
|
||||||
|
|
||||||
reader = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool Read ()
|
|
||||||
{
|
|
||||||
if (end_of_input)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
if (end_of_json) {
|
|
||||||
end_of_json = false;
|
|
||||||
automaton_stack.Clear ();
|
|
||||||
automaton_stack.Push ((int) ParserToken.End);
|
|
||||||
automaton_stack.Push ((int) ParserToken.Text);
|
|
||||||
}
|
|
||||||
|
|
||||||
parser_in_string = false;
|
|
||||||
parser_return = false;
|
|
||||||
|
|
||||||
token = JsonToken.None;
|
|
||||||
token_value = null;
|
|
||||||
|
|
||||||
if (! read_started) {
|
|
||||||
read_started = true;
|
|
||||||
|
|
||||||
if (! ReadToken ())
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
int[] entry_symbols;
|
|
||||||
|
|
||||||
while (true) {
|
|
||||||
if (parser_return) {
|
|
||||||
if (automaton_stack.Peek () == (int) ParserToken.End)
|
|
||||||
end_of_json = true;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
current_symbol = automaton_stack.Pop ();
|
|
||||||
|
|
||||||
ProcessSymbol ();
|
|
||||||
|
|
||||||
if (current_symbol == current_input) {
|
|
||||||
if (! ReadToken ()) {
|
|
||||||
if (automaton_stack.Peek () != (int) ParserToken.End)
|
|
||||||
throw new JsonException (
|
|
||||||
"Input doesn't evaluate to proper JSON text");
|
|
||||||
|
|
||||||
if (parser_return)
|
|
||||||
return true;
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
|
|
||||||
entry_symbols =
|
|
||||||
parse_table[current_symbol][current_input];
|
|
||||||
|
|
||||||
} catch (KeyNotFoundException e) {
|
|
||||||
throw new JsonException ((ParserToken) current_input, e);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (entry_symbols[0] == (int) ParserToken.Epsilon)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
for (int i = entry_symbols.Length - 1; i >= 0; i--)
|
|
||||||
automaton_stack.Push (entry_symbols[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Boolean SkipNonMembers { get; set; }
|
||||||
|
|
||||||
|
public Boolean EndOfInput { get; private set; }
|
||||||
|
|
||||||
|
public Boolean EndOfJson { get; private set; }
|
||||||
|
|
||||||
|
public JsonToken Token { get; private set; }
|
||||||
|
|
||||||
|
public Object Value { get; private set; }
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Constructors
|
||||||
|
static JsonReader() => parse_table = PopulateParseTable();
|
||||||
|
|
||||||
|
public JsonReader(String json_text) : this(new StringReader(json_text), true) { }
|
||||||
|
|
||||||
|
public JsonReader(TextReader reader) : this(reader, false) { }
|
||||||
|
|
||||||
|
private JsonReader(TextReader reader, Boolean owned) {
|
||||||
|
if(reader == null) {
|
||||||
|
throw new ArgumentNullException("reader");
|
||||||
|
}
|
||||||
|
|
||||||
|
this.parser_in_string = false;
|
||||||
|
this.parser_return = false;
|
||||||
|
|
||||||
|
this.read_started = false;
|
||||||
|
this.automaton_stack = new Stack<Int32>();
|
||||||
|
this.automaton_stack.Push((Int32)ParserToken.End);
|
||||||
|
this.automaton_stack.Push((Int32)ParserToken.Text);
|
||||||
|
|
||||||
|
this.lexer = new Lexer(reader);
|
||||||
|
|
||||||
|
this.EndOfInput = false;
|
||||||
|
this.EndOfJson = false;
|
||||||
|
|
||||||
|
this.SkipNonMembers = true;
|
||||||
|
|
||||||
|
this.reader = reader;
|
||||||
|
this.reader_is_owned = owned;
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Static Methods
|
||||||
|
private static IDictionary<Int32, IDictionary<Int32, Int32[]>> PopulateParseTable() =>
|
||||||
|
// See section A.2. of the manual for details
|
||||||
|
new Dictionary<Int32, IDictionary<Int32, Int32[]>> {
|
||||||
|
{
|
||||||
|
(Int32)ParserToken.Array,
|
||||||
|
new Dictionary<Int32, Int32[]> {
|
||||||
|
{ '[', new Int32[] { '[', (Int32)ParserToken.ArrayPrime } }
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
(Int32)ParserToken.ArrayPrime,
|
||||||
|
new Dictionary<Int32, Int32[]> {
|
||||||
|
{ '"', new Int32[] { (Int32)ParserToken.Value, (Int32)ParserToken.ValueRest, ']' } },
|
||||||
|
{ '[', new Int32[] { (Int32)ParserToken.Value, (Int32)ParserToken.ValueRest, ']' } },
|
||||||
|
{ ']', new Int32[] { ']' } },
|
||||||
|
{ '{', new Int32[] { (Int32)ParserToken.Value, (Int32)ParserToken.ValueRest, ']' } },
|
||||||
|
{ (Int32)ParserToken.Number, new Int32[] { (Int32)ParserToken.Value, (Int32)ParserToken.ValueRest, ']' } },
|
||||||
|
{ (Int32)ParserToken.True, new Int32[] { (Int32)ParserToken.Value, (Int32)ParserToken.ValueRest, ']' } },
|
||||||
|
{ (Int32)ParserToken.False, new Int32[] { (Int32)ParserToken.Value, (Int32)ParserToken.ValueRest, ']' } },
|
||||||
|
{ (Int32)ParserToken.Null, new Int32[] { (Int32)ParserToken.Value, (Int32)ParserToken.ValueRest, ']' } }
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
(Int32)ParserToken.Object,
|
||||||
|
new Dictionary<Int32, Int32[]> {
|
||||||
|
{ '{', new Int32[] { '{', (Int32)ParserToken.ObjectPrime } }
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
(Int32)ParserToken.ObjectPrime,
|
||||||
|
new Dictionary<Int32, Int32[]> {
|
||||||
|
{ '"', new Int32[] { (Int32)ParserToken.Pair, (Int32)ParserToken.PairRest, '}' } },
|
||||||
|
{ '}', new Int32[] { '}' } }
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
(Int32)ParserToken.Pair,
|
||||||
|
new Dictionary<Int32, Int32[]> {
|
||||||
|
{ '"', new Int32[] { (Int32)ParserToken.String, ':', (Int32)ParserToken.Value } }
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
(Int32)ParserToken.PairRest,
|
||||||
|
new Dictionary<Int32, Int32[]> {
|
||||||
|
{ ',', new Int32[] { ',', (Int32)ParserToken.Pair, (Int32)ParserToken.PairRest } },
|
||||||
|
{ '}', new Int32[] { (Int32)ParserToken.Epsilon } }
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
(Int32)ParserToken.String,
|
||||||
|
new Dictionary<Int32, Int32[]> {
|
||||||
|
{ '"', new Int32[] { '"', (Int32)ParserToken.CharSeq, '"' } }
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
(Int32)ParserToken.Text,
|
||||||
|
new Dictionary<Int32, Int32[]> {
|
||||||
|
{ '[', new Int32[] { (Int32)ParserToken.Array } },
|
||||||
|
{ '{', new Int32[] { (Int32)ParserToken.Object } }
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
(Int32)ParserToken.Value,
|
||||||
|
new Dictionary<Int32, Int32[]> {
|
||||||
|
{ '"', new Int32[] { (Int32)ParserToken.String } },
|
||||||
|
{ '[', new Int32[] { (Int32)ParserToken.Array } },
|
||||||
|
{ '{', new Int32[] { (Int32)ParserToken.Object } },
|
||||||
|
{ (Int32)ParserToken.Number, new Int32[] { (Int32)ParserToken.Number } },
|
||||||
|
{ (Int32)ParserToken.True, new Int32[] { (Int32)ParserToken.True } },
|
||||||
|
{ (Int32)ParserToken.False, new Int32[] { (Int32)ParserToken.False } },
|
||||||
|
{ (Int32)ParserToken.Null, new Int32[] { (Int32)ParserToken.Null } }
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
(Int32)ParserToken.ValueRest,
|
||||||
|
new Dictionary<Int32, Int32[]> {
|
||||||
|
{ ',', new Int32[] { ',', (Int32)ParserToken.Value, (Int32)ParserToken.ValueRest } },
|
||||||
|
{ ']', new Int32[] { (Int32)ParserToken.Epsilon } }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Private Methods
|
||||||
|
private void ProcessNumber(String number) {
|
||||||
|
if(number.IndexOf('.') != -1 || number.IndexOf('e') != -1 || number.IndexOf('E') != -1) {
|
||||||
|
if(Double.TryParse(number, NumberStyles.Any, CultureInfo.InvariantCulture, out Double n_double)) {
|
||||||
|
this.Token = JsonToken.Double;
|
||||||
|
this.Value = n_double;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(Int32.TryParse(number, NumberStyles.Integer, CultureInfo.InvariantCulture, out Int32 n_int32)) {
|
||||||
|
this.Token = JsonToken.Int;
|
||||||
|
this.Value = n_int32;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(Int64.TryParse(number, NumberStyles.Integer, CultureInfo.InvariantCulture, out Int64 n_int64)) {
|
||||||
|
this.Token = JsonToken.Long;
|
||||||
|
this.Value = n_int64;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(UInt64.TryParse(number, NumberStyles.Integer, CultureInfo.InvariantCulture, out UInt64 n_uint64)) {
|
||||||
|
this.Token = JsonToken.Long;
|
||||||
|
this.Value = n_uint64;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Shouldn't happen, but just in case, return something
|
||||||
|
this.Token = JsonToken.Int;
|
||||||
|
this.Value = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ProcessSymbol() {
|
||||||
|
if(this.current_symbol == '[') {
|
||||||
|
this.Token = JsonToken.ArrayStart;
|
||||||
|
this.parser_return = true;
|
||||||
|
} else if(this.current_symbol == ']') {
|
||||||
|
this.Token = JsonToken.ArrayEnd;
|
||||||
|
this.parser_return = true;
|
||||||
|
} else if(this.current_symbol == '{') {
|
||||||
|
this.Token = JsonToken.ObjectStart;
|
||||||
|
this.parser_return = true;
|
||||||
|
} else if(this.current_symbol == '}') {
|
||||||
|
this.Token = JsonToken.ObjectEnd;
|
||||||
|
this.parser_return = true;
|
||||||
|
} else if(this.current_symbol == '"') {
|
||||||
|
if(this.parser_in_string) {
|
||||||
|
this.parser_in_string = false;
|
||||||
|
this.parser_return = true;
|
||||||
|
} else {
|
||||||
|
if(this.Token == JsonToken.None) {
|
||||||
|
this.Token = JsonToken.String;
|
||||||
|
}
|
||||||
|
this.parser_in_string = true;
|
||||||
|
}
|
||||||
|
} else if(this.current_symbol == (Int32)ParserToken.CharSeq) {
|
||||||
|
this.Value = this.lexer.StringValue;
|
||||||
|
} else if(this.current_symbol == (Int32)ParserToken.False) {
|
||||||
|
this.Token = JsonToken.Boolean;
|
||||||
|
this.Value = false;
|
||||||
|
this.parser_return = true;
|
||||||
|
} else if(this.current_symbol == (Int32)ParserToken.Null) {
|
||||||
|
this.Token = JsonToken.Null;
|
||||||
|
this.parser_return = true;
|
||||||
|
} else if(this.current_symbol == (Int32)ParserToken.Number) {
|
||||||
|
this.ProcessNumber(this.lexer.StringValue);
|
||||||
|
this.parser_return = true;
|
||||||
|
} else if(this.current_symbol == (Int32)ParserToken.Pair) {
|
||||||
|
this.Token = JsonToken.PropertyName;
|
||||||
|
} else if(this.current_symbol == (Int32)ParserToken.True) {
|
||||||
|
this.Token = JsonToken.Boolean;
|
||||||
|
this.Value = true;
|
||||||
|
this.parser_return = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Boolean ReadToken() {
|
||||||
|
if(this.EndOfInput) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
_ = this.lexer.NextToken();
|
||||||
|
if(this.lexer.EndOfInput) {
|
||||||
|
this.Close();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
this.current_input = this.lexer.Token;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
public void Close() {
|
||||||
|
if(this.EndOfInput) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.EndOfInput = true;
|
||||||
|
this.EndOfJson = true;
|
||||||
|
if(this.reader_is_owned) {
|
||||||
|
using(this.reader) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.reader = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Boolean Read() {
|
||||||
|
if(this.EndOfInput) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if(this.EndOfJson) {
|
||||||
|
this.EndOfJson = false;
|
||||||
|
this.automaton_stack.Clear();
|
||||||
|
this.automaton_stack.Push((Int32)ParserToken.End);
|
||||||
|
this.automaton_stack.Push((Int32)ParserToken.Text);
|
||||||
|
}
|
||||||
|
this.parser_in_string = false;
|
||||||
|
this.parser_return = false;
|
||||||
|
this.Token = JsonToken.None;
|
||||||
|
this.Value = null;
|
||||||
|
if(!this.read_started) {
|
||||||
|
this.read_started = true;
|
||||||
|
if(!this.ReadToken()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Int32[] entry_symbols;
|
||||||
|
|
||||||
|
while(true) {
|
||||||
|
if(this.parser_return) {
|
||||||
|
if(this.automaton_stack.Peek() == (Int32)ParserToken.End) {
|
||||||
|
this.EndOfJson = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.current_symbol = this.automaton_stack.Pop();
|
||||||
|
|
||||||
|
this.ProcessSymbol();
|
||||||
|
|
||||||
|
if(this.current_symbol == this.current_input) {
|
||||||
|
if(!this.ReadToken()) {
|
||||||
|
if(this.automaton_stack.Peek() != (Int32)ParserToken.End) {
|
||||||
|
throw new JsonException(
|
||||||
|
"Input doesn't evaluate to proper JSON text");
|
||||||
|
}
|
||||||
|
|
||||||
|
if(this.parser_return) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
entry_symbols =
|
||||||
|
parse_table[this.current_symbol][this.current_input];
|
||||||
|
|
||||||
|
} catch(KeyNotFoundException e) {
|
||||||
|
throw new JsonException((ParserToken)this.current_input, e);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(entry_symbols[0] == (Int32)ParserToken.Epsilon) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
for(Int32 i = entry_symbols.Length - 1; i >= 0; i--) {
|
||||||
|
this.automaton_stack.Push(entry_symbols[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -51,14 +51,14 @@ namespace LitJson
|
|||||||
public Int32 IndentValue {
|
public Int32 IndentValue {
|
||||||
get => this.indent_value;
|
get => this.indent_value;
|
||||||
set {
|
set {
|
||||||
this.indentation = (this.indentation / this.indent_value) * value;
|
this.indentation = this.indentation / this.indent_value * value;
|
||||||
this.indent_value = value;
|
this.indent_value = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Boolean PrettyPrint { get; set; }
|
public Boolean PrettyPrint { get; set; }
|
||||||
|
|
||||||
public TextWriter TextWriter { get; }
|
private TextWriter TextWriter { get; }
|
||||||
|
|
||||||
public Boolean Validate { get; set; }
|
public Boolean Validate { get; set; }
|
||||||
|
|
||||||
@ -249,7 +249,7 @@ namespace LitJson
|
|||||||
this.ctx_stack.Push(this.context);
|
this.ctx_stack.Push(this.context);
|
||||||
|
|
||||||
if (this.inst_string_builder != null) {
|
if (this.inst_string_builder != null) {
|
||||||
this.inst_string_builder.Remove(0, this.inst_string_builder.Length);
|
_ = this.inst_string_builder.Remove(0, this.inst_string_builder.Length);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -316,6 +316,19 @@ namespace LitJson
|
|||||||
this.context.ExpectingValue = false;
|
this.context.ExpectingValue = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void WriteJson(String str) {
|
||||||
|
this.DoValidation(Condition.Value);
|
||||||
|
this.PutNewline();
|
||||||
|
|
||||||
|
if(str == null) {
|
||||||
|
this.Put("null");
|
||||||
|
} else {
|
||||||
|
this.TextWriter.Write(str);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.context.ExpectingValue = false;
|
||||||
|
}
|
||||||
|
|
||||||
public void Write(UInt64 number) {
|
public void Write(UInt64 number) {
|
||||||
this.DoValidation(Condition.Value);
|
this.DoValidation(Condition.Value);
|
||||||
this.PutNewline();
|
this.PutNewline();
|
||||||
@ -329,7 +342,7 @@ namespace LitJson
|
|||||||
this.DoValidation(Condition.InArray);
|
this.DoValidation(Condition.InArray);
|
||||||
this.PutNewline(false);
|
this.PutNewline(false);
|
||||||
|
|
||||||
this.ctx_stack.Pop();
|
_ = this.ctx_stack.Pop();
|
||||||
if (this.ctx_stack.Count == 1) {
|
if (this.ctx_stack.Count == 1) {
|
||||||
this.has_reached_end = true;
|
this.has_reached_end = true;
|
||||||
} else {
|
} else {
|
||||||
@ -359,7 +372,7 @@ namespace LitJson
|
|||||||
this.DoValidation(Condition.InObject);
|
this.DoValidation(Condition.InObject);
|
||||||
this.PutNewline(false);
|
this.PutNewline(false);
|
||||||
|
|
||||||
this.ctx_stack.Pop();
|
_ = this.ctx_stack.Pop();
|
||||||
if (this.ctx_stack.Count == 1) {
|
if (this.ctx_stack.Count == 1) {
|
||||||
this.has_reached_end = true;
|
this.has_reached_end = true;
|
||||||
} else {
|
} else {
|
||||||
|
1725
litjson/Lexer.cs
1725
litjson/Lexer.cs
File diff suppressed because it is too large
Load Diff
@ -9,36 +9,34 @@
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
namespace LitJson
|
namespace LitJson {
|
||||||
{
|
internal enum ParserToken {
|
||||||
internal enum ParserToken
|
// Lexer tokens (see section A.1.1. of the manual)
|
||||||
{
|
None = System.Char.MaxValue + 1,
|
||||||
// Lexer tokens (see section A.1.1. of the manual)
|
Number,
|
||||||
None = System.Char.MaxValue + 1,
|
True,
|
||||||
Number,
|
False,
|
||||||
True,
|
Null,
|
||||||
False,
|
CharSeq,
|
||||||
Null,
|
// Single char
|
||||||
CharSeq,
|
Char,
|
||||||
// Single char
|
|
||||||
Char,
|
|
||||||
|
|
||||||
// Parser Rules (see section A.2.1 of the manual)
|
// Parser Rules (see section A.2.1 of the manual)
|
||||||
Text,
|
Text,
|
||||||
Object,
|
Object,
|
||||||
ObjectPrime,
|
ObjectPrime,
|
||||||
Pair,
|
Pair,
|
||||||
PairRest,
|
PairRest,
|
||||||
Array,
|
Array,
|
||||||
ArrayPrime,
|
ArrayPrime,
|
||||||
Value,
|
Value,
|
||||||
ValueRest,
|
ValueRest,
|
||||||
String,
|
String,
|
||||||
|
|
||||||
// End of input
|
// End of input
|
||||||
End,
|
End,
|
||||||
|
|
||||||
// The empty rule
|
// The empty rule
|
||||||
Epsilon
|
Epsilon
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user