Refactoring and fixing a bug when add json text

This commit is contained in:
BlubbFish 2019-07-24 17:07:28 +02:00
parent aae12f70d3
commit cab3ec2d06
8 changed files with 1467 additions and 1626 deletions

View File

@ -11,16 +11,14 @@
#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, None,
Object, Object,
Array, Array,
String, String,
@ -30,31 +28,30 @@ namespace LitJson
Boolean Boolean
} }
public interface IJsonWrapper : IList, IOrderedDictionary public interface IJsonWrapper : IList, IOrderedDictionary {
{ Boolean IsArray { get; }
bool IsArray { get; } Boolean IsBoolean { get; }
bool IsBoolean { get; } Boolean IsDouble { get; }
bool IsDouble { get; } Boolean IsInt { get; }
bool IsInt { get; } Boolean IsLong { get; }
bool IsLong { get; } Boolean IsObject { get; }
bool IsObject { get; } Boolean IsString { get; }
bool IsString { get; }
bool GetBoolean (); Boolean GetBoolean();
double GetDouble (); Double GetDouble();
int GetInt (); Int32 GetInt();
JsonType GetJsonType(); JsonType GetJsonType();
long GetLong (); Int64 GetLong();
string GetString (); String GetString();
void SetBoolean (bool val); void SetBoolean(Boolean val);
void SetDouble (double val); void SetDouble(Double val);
void SetInt (int val); void SetInt(Int32 val);
void SetJsonType(JsonType type); void SetJsonType(JsonType type);
void SetLong (long val); void SetLong(Int64 val);
void SetString (string val); void SetString(String val);
string ToJson (); String ToJson();
void ToJson(JsonWriter writer); void ToJson(JsonWriter writer);
} }
} }

View File

@ -12,8 +12,7 @@
using System; using System;
namespace LitJson namespace LitJson {
{
public class JsonException : public class JsonException :
#if NETSTANDARD1_5 #if NETSTANDARD1_5
Exception Exception
@ -21,45 +20,18 @@ namespace LitJson
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)
{
}
} }
} }

View File

@ -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();

View File

@ -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 Boolean IsDouble => false;
public JsonType GetJsonType () { return JsonType.None; }
public long GetLong () { return 0L; } public Boolean IsInt => false;
public string GetString () { return ""; }
public Boolean IsLong => false;
public Boolean IsObject => false;
public Boolean IsString => false;
public Boolean GetBoolean() => false;
public Double GetDouble() => 0.0;
public Int32 GetInt() => 0;
public JsonType GetJsonType() => JsonType.None;
public Int64 GetLong() => 0L;
public String GetString() => "";
public void SetBoolean(Boolean val) { }
public void SetDouble(Double val) { }
public void SetInt(Int32 val) { }
public void SetBoolean (bool val) {}
public void SetDouble (double val) {}
public void SetInt (int val) {}
public void SetJsonType(JsonType type) { } public void SetJsonType(JsonType type) { }
public void SetLong (long val) {}
public void SetString (string val) {}
public string ToJson () { return ""; } public void SetLong(Int64 val) { }
public void SetString(String val) { }
public String ToJson() => "";
public void ToJson(JsonWriter writer) { } public void ToJson(JsonWriter writer) { }
Boolean IList.IsFixedSize => true;
bool IList.IsFixedSize { get { return true; } } Boolean IList.IsReadOnly => true;
bool IList.IsReadOnly { get { return true; } }
object IList.this[int index] { Object IList.this[Int32 index] {
get { return null; } get => null;
set {} set {
}
} }
int IList.Add (object value) { return 0; } Int32 IList.Add(Object value) => 0;
void IList.Clear() { } 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) {}
Boolean IList.Contains(Object value) => false;
int ICollection.Count { get { return 0; } } Int32 IList.IndexOf(Object value) => -1;
bool ICollection.IsSynchronized { get { return false; } }
object ICollection.SyncRoot { get { return null; } }
void ICollection.CopyTo (Array array, int index) {} void IList.Insert(Int32 i, Object v) { }
void IList.Remove(Object value) { }
IEnumerator IEnumerable.GetEnumerator () { return null; } void IList.RemoveAt(Int32 index) { }
Int32 ICollection.Count => 0;
bool IDictionary.IsFixedSize { get { return true; } } Boolean ICollection.IsSynchronized => false;
bool IDictionary.IsReadOnly { get { return true; } }
ICollection IDictionary.Keys { get { return null; } } Object ICollection.SyncRoot => null;
ICollection IDictionary.Values { get { return null; } }
object IDictionary.this[object key] { void ICollection.CopyTo(Array array, Int32 index) { }
get { return null; }
set {} 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.Add(Object k, Object v) { }
void IDictionary.Clear() { } void IDictionary.Clear() { }
bool IDictionary.Contains (object key) { return false; }
void IDictionary.Remove (object key) {}
IDictionaryEnumerator IDictionary.GetEnumerator () { return null; } Boolean IDictionary.Contains(Object key) => false;
void IDictionary.Remove(Object key) { }
object IOrderedDictionary.this[int idx] { IDictionaryEnumerator IDictionary.GetEnumerator() => null;
get { return null; }
set {}
}
IDictionaryEnumerator IOrderedDictionary.GetEnumerator () { Object IOrderedDictionary.this[Int32 idx] {
return null; get => null;
} set {
void IOrderedDictionary.Insert (int i, object k, object v) {} }
void IOrderedDictionary.RemoveAt (int i) {} }
IDictionaryEnumerator IOrderedDictionary.GetEnumerator() => null;
void IOrderedDictionary.Insert(Int32 i, Object k, Object v) { }
void IOrderedDictionary.RemoveAt(Int32 i) { }
} }
} }

View File

@ -16,440 +16,328 @@ using System.IO;
using System.Text; using System.Text;
namespace LitJson namespace LitJson {
{ public enum JsonToken {
public enum JsonToken
{
None, None,
ObjectStart, ObjectStart,
PropertyName, PropertyName,
ObjectEnd, ObjectEnd,
ArrayStart, ArrayStart,
ArrayEnd, ArrayEnd,
Int, Int,
Long, Long,
Double, Double,
String, String,
Boolean, Boolean,
Null Null
} }
public class JsonReader public class JsonReader {
{
#region Fields #region Fields
private static readonly IDictionary<int, IDictionary<int, int[]>> parse_table; private static readonly IDictionary<Int32, IDictionary<Int32, Int32[]>> parse_table;
private Stack<int> automaton_stack; private readonly Stack<Int32> automaton_stack;
private int current_input; private Int32 current_input;
private int current_symbol; private Int32 current_symbol;
private bool end_of_json; private readonly Lexer lexer;
private bool end_of_input; private Boolean parser_in_string;
private Lexer lexer; private Boolean parser_return;
private bool parser_in_string; private Boolean read_started;
private bool parser_return;
private bool read_started;
private TextReader reader; private TextReader reader;
private bool reader_is_owned; private readonly Boolean reader_is_owned;
private bool skip_non_members;
private object token_value;
private JsonToken token;
#endregion #endregion
#region Public Properties #region Public Properties
public bool AllowComments { public Boolean AllowComments {
get { return lexer.AllowComments; } get => this.lexer.AllowComments;
set { lexer.AllowComments = value; } set => this.lexer.AllowComments = value;
} }
public bool AllowSingleQuotedStrings { public Boolean AllowSingleQuotedStrings {
get { return lexer.AllowSingleQuotedStrings; } get => this.lexer.AllowSingleQuotedStrings;
set { lexer.AllowSingleQuotedStrings = value; } set => this.lexer.AllowSingleQuotedStrings = value;
} }
public bool SkipNonMembers { public Boolean SkipNonMembers { get; set; }
get { return skip_non_members; }
set { skip_non_members = value; }
}
public bool EndOfInput { public Boolean EndOfInput { get; private set; }
get { return end_of_input; }
}
public bool EndOfJson { public Boolean EndOfJson { get; private set; }
get { return end_of_json; }
}
public JsonToken Token { public JsonToken Token { get; private set; }
get { return token; }
}
public object Value { public Object Value { get; private set; }
get { return token_value; }
}
#endregion #endregion
#region Constructors #region Constructors
static JsonReader () static JsonReader() => parse_table = PopulateParseTable();
{
parse_table = PopulateParseTable ();
}
public JsonReader (string json_text) : public JsonReader(String json_text) : this(new StringReader(json_text), true) { }
this (new StringReader (json_text), true)
{
}
public JsonReader (TextReader reader) : public JsonReader(TextReader reader) : this(reader, false) { }
this (reader, false)
{
}
private JsonReader (TextReader reader, bool owned) private JsonReader(TextReader reader, Boolean owned) {
{ if(reader == null) {
if (reader == null)
throw new ArgumentNullException("reader"); throw new ArgumentNullException("reader");
}
parser_in_string = false; this.parser_in_string = false;
parser_return = false; this.parser_return = false;
read_started = false; this.read_started = false;
automaton_stack = new Stack<int> (); this.automaton_stack = new Stack<Int32>();
automaton_stack.Push ((int) ParserToken.End); this.automaton_stack.Push((Int32)ParserToken.End);
automaton_stack.Push ((int) ParserToken.Text); this.automaton_stack.Push((Int32)ParserToken.Text);
lexer = new Lexer (reader); this.lexer = new Lexer(reader);
end_of_input = false; this.EndOfInput = false;
end_of_json = false; this.EndOfJson = false;
skip_non_members = true; this.SkipNonMembers = true;
this.reader = reader; this.reader = reader;
reader_is_owned = owned; this.reader_is_owned = owned;
} }
#endregion #endregion
#region Static Methods #region Static Methods
private static IDictionary<int, IDictionary<int, int[]>> PopulateParseTable () private static IDictionary<Int32, IDictionary<Int32, Int32[]>> PopulateParseTable() =>
{
// See section A.2. of the manual for details // See section A.2. of the manual for details
IDictionary<int, IDictionary<int, int[]>> parse_table = new Dictionary<int, IDictionary<int, int[]>> (); new Dictionary<Int32, IDictionary<Int32, Int32[]>> {
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); (Int32)ParserToken.Array,
new Dictionary<Int32, Int32[]> {
{ '[', new Int32[] { '[', (Int32)ParserToken.ArrayPrime } }
} }
},
private static void TableAddRow (IDictionary<int, IDictionary<int, int[]>> parse_table, ParserToken rule)
{ {
parse_table.Add ((int) rule, new Dictionary<int, int[]> ()); (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 #endregion
#region Private Methods #region Private Methods
private void ProcessNumber (string number) private void ProcessNumber(String number) {
{ if(number.IndexOf('.') != -1 || number.IndexOf('e') != -1 || number.IndexOf('E') != -1) {
if (number.IndexOf ('.') != -1 || if(Double.TryParse(number, NumberStyles.Any, CultureInfo.InvariantCulture, out Double n_double)) {
number.IndexOf ('e') != -1 || this.Token = JsonToken.Double;
number.IndexOf ('E') != -1) { this.Value = n_double;
double n_double;
if (double.TryParse (number, NumberStyles.Any, CultureInfo.InvariantCulture, out n_double)) {
token = JsonToken.Double;
token_value = n_double;
return; return;
} }
} }
int n_int32; if(Int32.TryParse(number, NumberStyles.Integer, CultureInfo.InvariantCulture, out Int32 n_int32)) {
if (int.TryParse (number, NumberStyles.Integer, CultureInfo.InvariantCulture, out n_int32)) { this.Token = JsonToken.Int;
token = JsonToken.Int; this.Value = n_int32;
token_value = n_int32;
return; return;
} }
long n_int64; if(Int64.TryParse(number, NumberStyles.Integer, CultureInfo.InvariantCulture, out Int64 n_int64)) {
if (long.TryParse (number, NumberStyles.Integer, CultureInfo.InvariantCulture, out n_int64)) { this.Token = JsonToken.Long;
token = JsonToken.Long; this.Value = n_int64;
token_value = n_int64;
return; return;
} }
ulong n_uint64; if(UInt64.TryParse(number, NumberStyles.Integer, CultureInfo.InvariantCulture, out UInt64 n_uint64)) {
if (ulong.TryParse(number, NumberStyles.Integer, CultureInfo.InvariantCulture, out n_uint64)) this.Token = JsonToken.Long;
{ this.Value = n_uint64;
token = JsonToken.Long;
token_value = n_uint64;
return; return;
} }
// Shouldn't happen, but just in case, return something // Shouldn't happen, but just in case, return something
token = JsonToken.Int; this.Token = JsonToken.Int;
token_value = 0; this.Value = 0;
} }
private void ProcessSymbol () private void ProcessSymbol() {
{ if(this.current_symbol == '[') {
if (current_symbol == '[') { this.Token = JsonToken.ArrayStart;
token = JsonToken.ArrayStart; this.parser_return = true;
parser_return = true; } else if(this.current_symbol == ']') {
this.Token = JsonToken.ArrayEnd;
} else if (current_symbol == ']') { this.parser_return = true;
token = JsonToken.ArrayEnd; } else if(this.current_symbol == '{') {
parser_return = true; this.Token = JsonToken.ObjectStart;
this.parser_return = true;
} else if (current_symbol == '{') { } else if(this.current_symbol == '}') {
token = JsonToken.ObjectStart; this.Token = JsonToken.ObjectEnd;
parser_return = true; this.parser_return = true;
} else if(this.current_symbol == '"') {
} else if (current_symbol == '}') { if(this.parser_in_string) {
token = JsonToken.ObjectEnd; this.parser_in_string = false;
parser_return = true; this.parser_return = true;
} else if (current_symbol == '"') {
if (parser_in_string) {
parser_in_string = false;
parser_return = true;
} else { } else {
if (token == JsonToken.None) if(this.Token == JsonToken.None) {
token = JsonToken.String; this.Token = JsonToken.String;
parser_in_string = true;
} }
this.parser_in_string = true;
} else if (current_symbol == (int) ParserToken.CharSeq) { }
token_value = lexer.StringValue; } else if(this.current_symbol == (Int32)ParserToken.CharSeq) {
this.Value = this.lexer.StringValue;
} else if (current_symbol == (int) ParserToken.False) { } else if(this.current_symbol == (Int32)ParserToken.False) {
token = JsonToken.Boolean; this.Token = JsonToken.Boolean;
token_value = false; this.Value = false;
parser_return = true; this.parser_return = true;
} else if(this.current_symbol == (Int32)ParserToken.Null) {
} else if (current_symbol == (int) ParserToken.Null) { this.Token = JsonToken.Null;
token = JsonToken.Null; this.parser_return = true;
parser_return = true; } else if(this.current_symbol == (Int32)ParserToken.Number) {
this.ProcessNumber(this.lexer.StringValue);
} else if (current_symbol == (int) ParserToken.Number) { this.parser_return = true;
ProcessNumber (lexer.StringValue); } else if(this.current_symbol == (Int32)ParserToken.Pair) {
this.Token = JsonToken.PropertyName;
parser_return = true; } else if(this.current_symbol == (Int32)ParserToken.True) {
this.Token = JsonToken.Boolean;
} else if (current_symbol == (int) ParserToken.Pair) { this.Value = true;
token = JsonToken.PropertyName; this.parser_return = true;
} else if (current_symbol == (int) ParserToken.True) {
token = JsonToken.Boolean;
token_value = true;
parser_return = true;
} }
} }
private bool ReadToken () private Boolean ReadToken() {
{ if(this.EndOfInput) {
if (end_of_input)
return false;
lexer.NextToken ();
if (lexer.EndOfInput) {
Close ();
return false; return false;
} }
_ = this.lexer.NextToken();
current_input = lexer.Token; if(this.lexer.EndOfInput) {
this.Close();
return false;
}
this.current_input = this.lexer.Token;
return true; return true;
} }
#endregion #endregion
public void Close () public void Close() {
{ if(this.EndOfInput) {
if (end_of_input)
return; return;
}
end_of_input = true; this.EndOfInput = true;
end_of_json = true; this.EndOfJson = true;
if(this.reader_is_owned) {
if (reader_is_owned) using(this.reader) {
{ }
using(reader){} }
this.reader = null;
} }
reader = null; public Boolean Read() {
} if(this.EndOfInput) {
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; 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;
}
}
int[] entry_symbols; Int32[] entry_symbols;
while(true) { while(true) {
if (parser_return) { if(this.parser_return) {
if (automaton_stack.Peek () == (int) ParserToken.End) if(this.automaton_stack.Peek() == (Int32)ParserToken.End) {
end_of_json = true; this.EndOfJson = true;
}
return true; return true;
} }
current_symbol = automaton_stack.Pop (); this.current_symbol = this.automaton_stack.Pop();
ProcessSymbol (); this.ProcessSymbol();
if (current_symbol == current_input) { if(this.current_symbol == this.current_input) {
if (! ReadToken ()) { if(!this.ReadToken()) {
if (automaton_stack.Peek () != (int) ParserToken.End) if(this.automaton_stack.Peek() != (Int32)ParserToken.End) {
throw new JsonException( throw new JsonException(
"Input doesn't evaluate to proper JSON text"); "Input doesn't evaluate to proper JSON text");
}
if (parser_return) if(this.parser_return) {
return true; return true;
}
return false; return false;
} }
@ -460,17 +348,19 @@ namespace LitJson
try { try {
entry_symbols = entry_symbols =
parse_table[current_symbol][current_input]; parse_table[this.current_symbol][this.current_input];
} catch(KeyNotFoundException e) { } catch(KeyNotFoundException e) {
throw new JsonException ((ParserToken) current_input, e); throw new JsonException((ParserToken)this.current_input, e);
} }
if (entry_symbols[0] == (int) ParserToken.Epsilon) if(entry_symbols[0] == (Int32)ParserToken.Epsilon) {
continue; continue;
}
for (int i = entry_symbols.Length - 1; i >= 0; i--) for(Int32 i = entry_symbols.Length - 1; i >= 0; i--) {
automaton_stack.Push (entry_symbols[i]); this.automaton_stack.Push(entry_symbols[i]);
}
} }
} }

View File

@ -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 {

View File

@ -10,96 +10,66 @@
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Text; using System.Text;
namespace LitJson namespace LitJson {
{ internal class FsmContext {
internal class FsmContext public Boolean Return;
{ public Int32 NextState;
public bool Return;
public int NextState;
public Lexer L; public Lexer L;
public int StateStack; public Int32 StateStack;
} }
internal class Lexer {
internal class Lexer
{
#region Fields #region Fields
private delegate bool StateHandler (FsmContext ctx); private delegate Boolean StateHandler(FsmContext ctx);
private static readonly int[] fsm_return_table; private static readonly Int32[] fsm_return_table;
private static readonly StateHandler[] fsm_handler_table; private static readonly StateHandler[] fsm_handler_table;
private readonly FsmContext fsm_context;
private bool allow_comments; private Int32 input_buffer;
private bool allow_single_quoted_strings; private Int32 input_char;
private bool end_of_input; private readonly TextReader reader;
private FsmContext fsm_context; private Int32 state;
private int input_buffer; private readonly StringBuilder string_buffer;
private int input_char; private Int32 unichar;
private TextReader reader;
private int state;
private StringBuilder string_buffer;
private string string_value;
private int token;
private int unichar;
#endregion #endregion
#region Properties #region Properties
public bool AllowComments { public Boolean AllowComments { get; set; }
get { return allow_comments; }
set { allow_comments = value; }
}
public bool AllowSingleQuotedStrings { public Boolean AllowSingleQuotedStrings { get; set; }
get { return allow_single_quoted_strings; }
set { allow_single_quoted_strings = value; }
}
public bool EndOfInput { public Boolean EndOfInput { get; private set; }
get { return end_of_input; }
}
public int Token { public Int32 Token { get; private set; }
get { return token; }
}
public string StringValue { public String StringValue { get; private set; }
get { return string_value; }
}
#endregion #endregion
#region Constructors #region Constructors
static Lexer () static Lexer() => PopulateFsmTables(out fsm_handler_table, out fsm_return_table);
{
PopulateFsmTables (out fsm_handler_table, out fsm_return_table);
}
public Lexer (TextReader reader) public Lexer(TextReader reader) {
{ this.AllowComments = true;
allow_comments = true; this.AllowSingleQuotedStrings = true;
allow_single_quoted_strings = true;
input_buffer = 0; this.input_buffer = 0;
string_buffer = new StringBuilder (128); this.string_buffer = new StringBuilder(128);
state = 1; this.state = 1;
end_of_input = false; this.EndOfInput = false;
this.reader = reader; this.reader = reader;
fsm_context = new FsmContext (); this.fsm_context = new FsmContext {
fsm_context.L = this; L = this
};
} }
#endregion #endregion
#region Static Methods #region Static Methods
private static int HexValue (int digit) private static Int32 HexValue(Int32 digit) {
{
switch(digit) { switch(digit) {
case 'a': case 'a':
case 'A': case 'A':
@ -130,8 +100,7 @@ namespace LitJson
} }
} }
private static void PopulateFsmTables (out StateHandler[] fsm_handler_table, out int[] fsm_return_table) private static void PopulateFsmTables(out StateHandler[] fsm_handler_table, out Int32[] fsm_return_table) {
{
// See section A.1. of the manual for details of the finite // See section A.1. of the manual for details of the finite
// state machine. // state machine.
fsm_handler_table = new StateHandler[28] { fsm_handler_table = new StateHandler[28] {
@ -165,31 +134,31 @@ namespace LitJson
State28 State28
}; };
fsm_return_table = new int[28] { fsm_return_table = new Int32[28] {
(int) ParserToken.Char, (Int32) ParserToken.Char,
0, 0,
(int) ParserToken.Number, (Int32) ParserToken.Number,
(int) ParserToken.Number, (Int32) ParserToken.Number,
0, 0,
(int) ParserToken.Number, (Int32) ParserToken.Number,
0, 0,
(int) ParserToken.Number, (Int32) ParserToken.Number,
0, 0,
0, 0,
(int) ParserToken.True, (Int32) ParserToken.True,
0, 0,
0, 0,
0, 0,
(int) ParserToken.False, (Int32) ParserToken.False,
0, 0,
0, 0,
(int) ParserToken.Null, (Int32) ParserToken.Null,
(int) ParserToken.CharSeq, (Int32) ParserToken.CharSeq,
(int) ParserToken.Char, (Int32) ParserToken.Char,
0, 0,
0, 0,
(int) ParserToken.CharSeq, (Int32) ParserToken.CharSeq,
(int) ParserToken.Char, (Int32) ParserToken.Char,
0, 0,
0, 0,
0, 0,
@ -197,8 +166,7 @@ namespace LitJson
}; };
} }
private static char ProcessEscChar (int esc_char) private static Char ProcessEscChar(Int32 esc_char) {
{
switch(esc_char) { switch(esc_char) {
case '"': case '"':
case '\'': case '\'':
@ -227,15 +195,15 @@ namespace LitJson
} }
} }
private static bool State1 (FsmContext ctx) private static Boolean State1(FsmContext ctx) {
{
while(ctx.L.GetChar()) { while(ctx.L.GetChar()) {
if(ctx.L.input_char == ' ' || if(ctx.L.input_char == ' ' ||
ctx.L.input_char >= '\t' && ctx.L.input_char <= '\r') ctx.L.input_char >= '\t' && ctx.L.input_char <= '\r') {
continue; continue;
}
if(ctx.L.input_char >= '1' && ctx.L.input_char <= '9') { if(ctx.L.input_char >= '1' && ctx.L.input_char <= '9') {
ctx.L.string_buffer.Append ((char) ctx.L.input_char); _ = ctx.L.string_buffer.Append((Char)ctx.L.input_char);
ctx.NextState = 3; ctx.NextState = 3;
return true; return true;
} }
@ -257,12 +225,12 @@ namespace LitJson
return true; return true;
case '-': case '-':
ctx.L.string_buffer.Append ((char) ctx.L.input_char); _ = ctx.L.string_buffer.Append((Char)ctx.L.input_char);
ctx.NextState = 2; ctx.NextState = 2;
return true; return true;
case '0': case '0':
ctx.L.string_buffer.Append ((char) ctx.L.input_char); _ = ctx.L.string_buffer.Append((Char)ctx.L.input_char);
ctx.NextState = 4; ctx.NextState = 4;
return true; return true;
@ -279,8 +247,9 @@ namespace LitJson
return true; return true;
case '\'': case '\'':
if (! ctx.L.allow_single_quoted_strings) if(!ctx.L.AllowSingleQuotedStrings) {
return false; return false;
}
ctx.L.input_char = '"'; ctx.L.input_char = '"';
ctx.NextState = 23; ctx.NextState = 23;
@ -288,8 +257,9 @@ namespace LitJson
return true; return true;
case '/': case '/':
if (! ctx.L.allow_comments) if(!ctx.L.AllowComments) {
return false; return false;
}
ctx.NextState = 25; ctx.NextState = 25;
return true; return true;
@ -302,19 +272,18 @@ namespace LitJson
return true; return true;
} }
private static bool State2 (FsmContext ctx) private static Boolean State2(FsmContext ctx) {
{ _ = ctx.L.GetChar();
ctx.L.GetChar ();
if(ctx.L.input_char >= '1' && ctx.L.input_char <= '9') { if(ctx.L.input_char >= '1' && ctx.L.input_char <= '9') {
ctx.L.string_buffer.Append ((char) ctx.L.input_char); _ = ctx.L.string_buffer.Append((Char)ctx.L.input_char);
ctx.NextState = 3; ctx.NextState = 3;
return true; return true;
} }
switch(ctx.L.input_char) { switch(ctx.L.input_char) {
case '0': case '0':
ctx.L.string_buffer.Append ((char) ctx.L.input_char); _ = ctx.L.string_buffer.Append((Char)ctx.L.input_char);
ctx.NextState = 4; ctx.NextState = 4;
return true; return true;
@ -323,11 +292,10 @@ namespace LitJson
} }
} }
private static bool State3 (FsmContext ctx) private static Boolean State3(FsmContext ctx) {
{
while(ctx.L.GetChar()) { while(ctx.L.GetChar()) {
if(ctx.L.input_char >= '0' && ctx.L.input_char <= '9') { if(ctx.L.input_char >= '0' && ctx.L.input_char <= '9') {
ctx.L.string_buffer.Append ((char) ctx.L.input_char); _ = ctx.L.string_buffer.Append((Char)ctx.L.input_char);
continue; continue;
} }
@ -348,13 +316,13 @@ namespace LitJson
return true; return true;
case '.': case '.':
ctx.L.string_buffer.Append ((char) ctx.L.input_char); _ = ctx.L.string_buffer.Append((Char)ctx.L.input_char);
ctx.NextState = 5; ctx.NextState = 5;
return true; return true;
case 'e': case 'e':
case 'E': case 'E':
ctx.L.string_buffer.Append ((char) ctx.L.input_char); _ = ctx.L.string_buffer.Append((Char)ctx.L.input_char);
ctx.NextState = 7; ctx.NextState = 7;
return true; return true;
@ -365,9 +333,8 @@ namespace LitJson
return true; return true;
} }
private static bool State4 (FsmContext ctx) private static Boolean State4(FsmContext ctx) {
{ _ = ctx.L.GetChar();
ctx.L.GetChar ();
if(ctx.L.input_char == ' ' || if(ctx.L.input_char == ' ' ||
ctx.L.input_char >= '\t' && ctx.L.input_char <= '\r') { ctx.L.input_char >= '\t' && ctx.L.input_char <= '\r') {
@ -386,13 +353,13 @@ namespace LitJson
return true; return true;
case '.': case '.':
ctx.L.string_buffer.Append ((char) ctx.L.input_char); _ = ctx.L.string_buffer.Append((Char)ctx.L.input_char);
ctx.NextState = 5; ctx.NextState = 5;
return true; return true;
case 'e': case 'e':
case 'E': case 'E':
ctx.L.string_buffer.Append ((char) ctx.L.input_char); _ = ctx.L.string_buffer.Append((Char)ctx.L.input_char);
ctx.NextState = 7; ctx.NextState = 7;
return true; return true;
@ -401,12 +368,11 @@ namespace LitJson
} }
} }
private static bool State5 (FsmContext ctx) private static Boolean State5(FsmContext ctx) {
{ _ = ctx.L.GetChar();
ctx.L.GetChar ();
if(ctx.L.input_char >= '0' && ctx.L.input_char <= '9') { if(ctx.L.input_char >= '0' && ctx.L.input_char <= '9') {
ctx.L.string_buffer.Append ((char) ctx.L.input_char); _ = ctx.L.string_buffer.Append((Char)ctx.L.input_char);
ctx.NextState = 6; ctx.NextState = 6;
return true; return true;
} }
@ -414,11 +380,10 @@ namespace LitJson
return false; return false;
} }
private static bool State6 (FsmContext ctx) private static Boolean State6(FsmContext ctx) {
{
while(ctx.L.GetChar()) { while(ctx.L.GetChar()) {
if(ctx.L.input_char >= '0' && ctx.L.input_char <= '9') { if(ctx.L.input_char >= '0' && ctx.L.input_char <= '9') {
ctx.L.string_buffer.Append ((char) ctx.L.input_char); _ = ctx.L.string_buffer.Append((Char)ctx.L.input_char);
continue; continue;
} }
@ -440,7 +405,7 @@ namespace LitJson
case 'e': case 'e':
case 'E': case 'E':
ctx.L.string_buffer.Append ((char) ctx.L.input_char); _ = ctx.L.string_buffer.Append((Char)ctx.L.input_char);
ctx.NextState = 7; ctx.NextState = 7;
return true; return true;
@ -452,12 +417,11 @@ namespace LitJson
return true; return true;
} }
private static bool State7 (FsmContext ctx) private static Boolean State7(FsmContext ctx) {
{ _ = ctx.L.GetChar();
ctx.L.GetChar ();
if(ctx.L.input_char >= '0' && ctx.L.input_char <= '9') { if(ctx.L.input_char >= '0' && ctx.L.input_char <= '9') {
ctx.L.string_buffer.Append ((char) ctx.L.input_char); _ = ctx.L.string_buffer.Append((Char)ctx.L.input_char);
ctx.NextState = 8; ctx.NextState = 8;
return true; return true;
} }
@ -465,7 +429,7 @@ namespace LitJson
switch(ctx.L.input_char) { switch(ctx.L.input_char) {
case '+': case '+':
case '-': case '-':
ctx.L.string_buffer.Append ((char) ctx.L.input_char); _ = ctx.L.string_buffer.Append((Char)ctx.L.input_char);
ctx.NextState = 8; ctx.NextState = 8;
return true; return true;
@ -474,11 +438,10 @@ namespace LitJson
} }
} }
private static bool State8 (FsmContext ctx) private static Boolean State8(FsmContext ctx) {
{
while(ctx.L.GetChar()) { while(ctx.L.GetChar()) {
if(ctx.L.input_char >= '0' && ctx.L.input_char <= '9') { if(ctx.L.input_char >= '0' && ctx.L.input_char <= '9') {
ctx.L.string_buffer.Append ((char) ctx.L.input_char); _ = ctx.L.string_buffer.Append((Char)ctx.L.input_char);
continue; continue;
} }
@ -506,9 +469,8 @@ namespace LitJson
return true; return true;
} }
private static bool State9 (FsmContext ctx) private static Boolean State9(FsmContext ctx) {
{ _ = ctx.L.GetChar();
ctx.L.GetChar ();
switch(ctx.L.input_char) { switch(ctx.L.input_char) {
case 'r': case 'r':
@ -520,9 +482,8 @@ namespace LitJson
} }
} }
private static bool State10 (FsmContext ctx) private static Boolean State10(FsmContext ctx) {
{ _ = ctx.L.GetChar();
ctx.L.GetChar ();
switch(ctx.L.input_char) { switch(ctx.L.input_char) {
case 'u': case 'u':
@ -534,9 +495,8 @@ namespace LitJson
} }
} }
private static bool State11 (FsmContext ctx) private static Boolean State11(FsmContext ctx) {
{ _ = ctx.L.GetChar();
ctx.L.GetChar ();
switch(ctx.L.input_char) { switch(ctx.L.input_char) {
case 'e': case 'e':
@ -549,9 +509,8 @@ namespace LitJson
} }
} }
private static bool State12 (FsmContext ctx) private static Boolean State12(FsmContext ctx) {
{ _ = ctx.L.GetChar();
ctx.L.GetChar ();
switch(ctx.L.input_char) { switch(ctx.L.input_char) {
case 'a': case 'a':
@ -563,9 +522,8 @@ namespace LitJson
} }
} }
private static bool State13 (FsmContext ctx) private static Boolean State13(FsmContext ctx) {
{ _ = ctx.L.GetChar();
ctx.L.GetChar ();
switch(ctx.L.input_char) { switch(ctx.L.input_char) {
case 'l': case 'l':
@ -577,9 +535,8 @@ namespace LitJson
} }
} }
private static bool State14 (FsmContext ctx) private static Boolean State14(FsmContext ctx) {
{ _ = ctx.L.GetChar();
ctx.L.GetChar ();
switch(ctx.L.input_char) { switch(ctx.L.input_char) {
case 's': case 's':
@ -591,9 +548,8 @@ namespace LitJson
} }
} }
private static bool State15 (FsmContext ctx) private static Boolean State15(FsmContext ctx) {
{ _ = ctx.L.GetChar();
ctx.L.GetChar ();
switch(ctx.L.input_char) { switch(ctx.L.input_char) {
case 'e': case 'e':
@ -606,9 +562,8 @@ namespace LitJson
} }
} }
private static bool State16 (FsmContext ctx) private static Boolean State16(FsmContext ctx) {
{ _ = ctx.L.GetChar();
ctx.L.GetChar ();
switch(ctx.L.input_char) { switch(ctx.L.input_char) {
case 'u': case 'u':
@ -620,9 +575,8 @@ namespace LitJson
} }
} }
private static bool State17 (FsmContext ctx) private static Boolean State17(FsmContext ctx) {
{ _ = ctx.L.GetChar();
ctx.L.GetChar ();
switch(ctx.L.input_char) { switch(ctx.L.input_char) {
case 'l': case 'l':
@ -634,9 +588,8 @@ namespace LitJson
} }
} }
private static bool State18 (FsmContext ctx) private static Boolean State18(FsmContext ctx) {
{ _ = ctx.L.GetChar();
ctx.L.GetChar ();
switch(ctx.L.input_char) { switch(ctx.L.input_char) {
case 'l': case 'l':
@ -649,8 +602,7 @@ namespace LitJson
} }
} }
private static bool State19 (FsmContext ctx) private static Boolean State19(FsmContext ctx) {
{
while(ctx.L.GetChar()) { while(ctx.L.GetChar()) {
switch(ctx.L.input_char) { switch(ctx.L.input_char) {
case '"': case '"':
@ -665,7 +617,7 @@ namespace LitJson
return true; return true;
default: default:
ctx.L.string_buffer.Append ((char) ctx.L.input_char); _ = ctx.L.string_buffer.Append((Char)ctx.L.input_char);
continue; continue;
} }
} }
@ -673,9 +625,8 @@ namespace LitJson
return true; return true;
} }
private static bool State20 (FsmContext ctx) private static Boolean State20(FsmContext ctx) {
{ _ = ctx.L.GetChar();
ctx.L.GetChar ();
switch(ctx.L.input_char) { switch(ctx.L.input_char) {
case '"': case '"':
@ -688,9 +639,8 @@ namespace LitJson
} }
} }
private static bool State21 (FsmContext ctx) private static Boolean State21(FsmContext ctx) {
{ _ = ctx.L.GetChar();
ctx.L.GetChar ();
switch(ctx.L.input_char) { switch(ctx.L.input_char) {
case 'u': case 'u':
@ -706,7 +656,7 @@ namespace LitJson
case 'n': case 'n':
case 'r': case 'r':
case 't': case 't':
ctx.L.string_buffer.Append ( _ = ctx.L.string_buffer.Append(
ProcessEscChar(ctx.L.input_char)); ProcessEscChar(ctx.L.input_char));
ctx.NextState = ctx.StateStack; ctx.NextState = ctx.StateStack;
return true; return true;
@ -716,10 +666,9 @@ namespace LitJson
} }
} }
private static bool State22 (FsmContext ctx) private static Boolean State22(FsmContext ctx) {
{ Int32 counter = 0;
int counter = 0; Int32 mult = 4096;
int mult = 4096;
ctx.L.unichar = 0; ctx.L.unichar = 0;
@ -735,7 +684,7 @@ namespace LitJson
mult /= 16; mult /= 16;
if(counter == 4) { if(counter == 4) {
ctx.L.string_buffer.Append ( _ = ctx.L.string_buffer.Append(
Convert.ToChar(ctx.L.unichar)); Convert.ToChar(ctx.L.unichar));
ctx.NextState = ctx.StateStack; ctx.NextState = ctx.StateStack;
return true; return true;
@ -750,8 +699,7 @@ namespace LitJson
return true; return true;
} }
private static bool State23 (FsmContext ctx) private static Boolean State23(FsmContext ctx) {
{
while(ctx.L.GetChar()) { while(ctx.L.GetChar()) {
switch(ctx.L.input_char) { switch(ctx.L.input_char) {
case '\'': case '\'':
@ -766,7 +714,7 @@ namespace LitJson
return true; return true;
default: default:
ctx.L.string_buffer.Append ((char) ctx.L.input_char); _ = ctx.L.string_buffer.Append((Char)ctx.L.input_char);
continue; continue;
} }
} }
@ -774,9 +722,8 @@ namespace LitJson
return true; return true;
} }
private static bool State24 (FsmContext ctx) private static Boolean State24(FsmContext ctx) {
{ _ = ctx.L.GetChar();
ctx.L.GetChar ();
switch(ctx.L.input_char) { switch(ctx.L.input_char) {
case '\'': case '\'':
@ -790,9 +737,8 @@ namespace LitJson
} }
} }
private static bool State25 (FsmContext ctx) private static Boolean State25(FsmContext ctx) {
{ _ = ctx.L.GetChar();
ctx.L.GetChar ();
switch(ctx.L.input_char) { switch(ctx.L.input_char) {
case '*': case '*':
@ -808,8 +754,7 @@ namespace LitJson
} }
} }
private static bool State26 (FsmContext ctx) private static Boolean State26(FsmContext ctx) {
{
while(ctx.L.GetChar()) { while(ctx.L.GetChar()) {
if(ctx.L.input_char == '\n') { if(ctx.L.input_char == '\n') {
ctx.NextState = 1; ctx.NextState = 1;
@ -820,8 +765,7 @@ namespace LitJson
return true; return true;
} }
private static bool State27 (FsmContext ctx) private static Boolean State27(FsmContext ctx) {
{
while(ctx.L.GetChar()) { while(ctx.L.GetChar()) {
if(ctx.L.input_char == '*') { if(ctx.L.input_char == '*') {
ctx.NextState = 28; ctx.NextState = 28;
@ -832,11 +776,11 @@ namespace LitJson
return true; return true;
} }
private static bool State28 (FsmContext ctx) private static Boolean State28(FsmContext ctx) {
{
while(ctx.L.GetChar()) { while(ctx.L.GetChar()) {
if (ctx.L.input_char == '*') if(ctx.L.input_char == '*') {
continue; continue;
}
if(ctx.L.input_char == '/') { if(ctx.L.input_char == '/') {
ctx.NextState = 1; ctx.NextState = 1;
@ -851,62 +795,59 @@ namespace LitJson
} }
#endregion #endregion
private Boolean GetChar() {
private bool GetChar () if((this.input_char = this.NextChar()) != -1) {
{
if ((input_char = NextChar ()) != -1)
return true; return true;
}
end_of_input = true; this.EndOfInput = true;
return false; return false;
} }
private int NextChar () private Int32 NextChar() {
{ if(this.input_buffer != 0) {
if (input_buffer != 0) { Int32 tmp = this.input_buffer;
int tmp = input_buffer; this.input_buffer = 0;
input_buffer = 0;
return tmp; return tmp;
} }
return reader.Read (); return this.reader.Read();
} }
public bool NextToken () public Boolean NextToken() {
{
StateHandler handler; StateHandler handler;
fsm_context.Return = false; this.fsm_context.Return = false;
while(true) { while(true) {
handler = fsm_handler_table[state - 1]; handler = fsm_handler_table[this.state - 1];
if (! handler (fsm_context)) if(!handler(this.fsm_context)) {
throw new JsonException (input_char); throw new JsonException(this.input_char);
}
if (end_of_input) if(this.EndOfInput) {
return false; return false;
}
if (fsm_context.Return) { if(this.fsm_context.Return) {
string_value = string_buffer.ToString (); this.StringValue = this.string_buffer.ToString();
string_buffer.Remove (0, string_buffer.Length); _ = this.string_buffer.Remove(0, this.string_buffer.Length);
token = fsm_return_table[state - 1]; this.Token = fsm_return_table[this.state - 1];
if (token == (int) ParserToken.Char) if(this.Token == (Int32)ParserToken.Char) {
token = input_char; this.Token = this.input_char;
}
state = fsm_context.NextState; this.state = this.fsm_context.NextState;
return true; return true;
} }
state = fsm_context.NextState; this.state = this.fsm_context.NextState;
} }
} }
private void UngetChar () private void UngetChar() => this.input_buffer = this.input_char;
{
input_buffer = input_char;
}
} }
} }

View File

@ -9,10 +9,8 @@
#endregion #endregion
namespace LitJson namespace LitJson {
{ internal enum ParserToken {
internal enum ParserToken
{
// Lexer tokens (see section A.1.1. of the manual) // Lexer tokens (see section A.1.1. of the manual)
None = System.Char.MaxValue + 1, None = System.Char.MaxValue + 1,
Number, Number,