private static void _TableProc(Node node, string key, Node value) { ((Dictionary<string, Node>)node)[key] = value; }
private static void _ArrayProc(Node node, string key, Node value) { ((List<Node>)node).Add(value); }
private bool Write(Node node) { switch (node.GetKind()) { case Node.Kind.NULL: writer.WriteNull(); return true; case Node.Kind.BOOLEAN: writer.WriteBool((bool)node); return true; case Node.Kind.INT: writer.WriteInt((int)node); return true; case Node.Kind.UINT: writer.WriteUInt((uint)node); return true; case Node.Kind.DOUBLE: writer.WriteNumber((double)node); return true; case Node.Kind.STRING: writer.WriteString((string)node); return true; case Node.Kind.ARRAY: { if (writed.ContainsKey(node)) return false; writed.Add(node, true); List<Node> list = (List<Node>)node; writer.WriteNewArray(list.Count); bool first = true; for (int i = 0, j = list.Count; i < j; ++i) { if (first) { first = false; } else { writer.WriteComma(); } writer.WriteIndent(); if (!Write(list[i])) return false; } writer.WriteCloseArray(list.Count); return true; } case Node.Kind.TABLE: { if (writed.ContainsKey(node)) return false; writed.Add(node, true); Dictionary<string, Node> table = (Dictionary<string, Node>)node; writer.WriteNewTable(table.Count); bool first = true; foreach (KeyValuePair<string, Node> kv in table) { if (first) { first = false; } else { writer.WriteComma(); } writer.WriteIndent(); writer.WriteTableKey(kv.Key); if (!Write(kv.Value)) return false; } writer.WriteCloseTable(table.Count); return true; } } return false; }
public string String(Node node) { writer.Reset(); bool result = Write(node); writed.Clear(); if (!result) return null; return writer.ToString(); }
public byte[] Bytes(Node node) { string str = String(node); if (str == null) return null; return Encoding.UTF8.GetBytes(str); }
private bool Value(Node value) { if (context.root == null) { context.root = value; return true; } else if (context.nodeprocs.Count > 0) { NodeProc nodeproc = context.nodeprocs[context.nodeprocs.Count - 1]; nodeproc.proc(nodeproc.node, context.key, value); return true; } context.Error(); return false; }