Initial commit
This commit is contained in:
+140
@@ -0,0 +1,140 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Server
|
||||
{
|
||||
public static class MiniJson
|
||||
{
|
||||
public static object Parse(string json)
|
||||
{
|
||||
return new Parser(json).ParseValue();
|
||||
}
|
||||
|
||||
private class Parser
|
||||
{
|
||||
private readonly string _json;
|
||||
private int _index;
|
||||
|
||||
public Parser(string json)
|
||||
{
|
||||
_json = json;
|
||||
}
|
||||
|
||||
public object ParseValue()
|
||||
{
|
||||
SkipWhitespace();
|
||||
|
||||
if (_index >= _json.Length)
|
||||
return null;
|
||||
|
||||
char c = _json[_index];
|
||||
|
||||
if (c == '{') return ParseObject();
|
||||
if (c == '[') return ParseArray();
|
||||
if (c == '"') return ParseString();
|
||||
if (char.IsDigit(c) || c == '-') return ParseNumber();
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private Dictionary<string, object> ParseObject()
|
||||
{
|
||||
var dict = new Dictionary<string, object>();
|
||||
_index++; // skip {
|
||||
|
||||
while (true)
|
||||
{
|
||||
SkipWhitespace();
|
||||
if (_json[_index] == '}')
|
||||
{
|
||||
_index++;
|
||||
return dict;
|
||||
}
|
||||
|
||||
string key = ParseString();
|
||||
|
||||
SkipWhitespace();
|
||||
_index++; // skip :
|
||||
|
||||
object value = ParseValue();
|
||||
dict[key] = value;
|
||||
|
||||
SkipWhitespace();
|
||||
if (_json[_index] == ',')
|
||||
{
|
||||
_index++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (_json[_index] == '}')
|
||||
{
|
||||
_index++;
|
||||
return dict;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private List<object> ParseArray()
|
||||
{
|
||||
var list = new List<object>();
|
||||
_index++; // skip [
|
||||
|
||||
while (true)
|
||||
{
|
||||
SkipWhitespace();
|
||||
if (_json[_index] == ']')
|
||||
{
|
||||
_index++;
|
||||
return list;
|
||||
}
|
||||
|
||||
list.Add(ParseValue());
|
||||
|
||||
SkipWhitespace();
|
||||
if (_json[_index] == ',')
|
||||
{
|
||||
_index++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (_json[_index] == ']')
|
||||
{
|
||||
_index++;
|
||||
return list;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private string ParseString()
|
||||
{
|
||||
_index++; // skip "
|
||||
int start = _index;
|
||||
|
||||
while (_json[_index] != '"')
|
||||
_index++;
|
||||
|
||||
string s = _json.Substring(start, _index - start);
|
||||
_index++; // skip closing "
|
||||
return s;
|
||||
}
|
||||
|
||||
private object ParseNumber()
|
||||
{
|
||||
int start = _index;
|
||||
|
||||
while (_index < _json.Length && (char.IsDigit(_json[_index]) || _json[_index] == '-'))
|
||||
_index++;
|
||||
|
||||
string num = _json.Substring(start, _index - start);
|
||||
return int.Parse(num);
|
||||
}
|
||||
|
||||
private void SkipWhitespace()
|
||||
{
|
||||
while (_index < _json.Length && char.IsWhiteSpace(_json[_index]))
|
||||
_index++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user