VoxelUnity/Assets/Scripts/JSONStreamReader.cs

65 lines
1.4 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class JSONStreamReader
{
private string m_stream = "";
private Queue<string> m_json_objects;
private int m_bracket_depth = 0;
public bool is_empty
{
get
{
return m_json_objects.Count == 0;
}
}
public JSONStreamReader()
{
m_json_objects = new Queue<string>();
}
public string Pop()
{
return m_json_objects.Dequeue();
}
public void AddData(string s)
{
int counter = 0;
foreach(char c in s)
{
counter++;
if (c == '{')
{
m_bracket_depth++;
}
else if (c == '}')
{
m_bracket_depth--;
if (m_bracket_depth < 0)
{
throw new System.Exception("parsed bad json data");
}
if (m_bracket_depth == 0)
{
m_stream += s.Substring(0, counter);
// push string to queue
m_json_objects.Enqueue(m_stream);
// process remaining data
m_stream = "";
AddData(s.Substring(counter));
return;
}
}
}
m_stream += s;
}
}