diff Implab/JSON/JSONXmlReader.cs @ 60:10c7337d29e7

+JSONXmlReaderOptions *JSONScanner: fixed reading from TextReader *code cleanup
author cin
date Thu, 19 Jun 2014 03:41:28 +0400
parents 21611344d366
children 908b4f340c69
line wrap: on
line diff
--- a/Implab/JSON/JSONXmlReader.cs	Wed Jun 18 03:54:02 2014 +0400
+++ b/Implab/JSON/JSONXmlReader.cs	Thu Jun 19 03:41:28 2014 +0400
@@ -1,14 +1,14 @@
 using Implab;
-using Implab.JSON;
 using Implab.Parsing;
 using System;
 using System.Collections.Generic;
+using System.IO;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.Xml;
 
-namespace ConsPlay {
+namespace Implab.JSON {
     public class JSONXmlReader : XmlReader {
 
         enum ValueContext {
@@ -29,16 +29,34 @@
         ReadState m_state = ReadState.Initial;
         Stack<LocalNameContext> m_localNameStack = new Stack<LocalNameContext>();
         LocalNameContext m_localName;
-        string m_rootName = "json";
-        string m_prefix = String.Empty;
-        string m_namespaceUri = String.Empty;
-        bool m_flattenArrays = false;
-        NameTable m_nameTable = new NameTable();
         int m_depthCorrection = 0;
 
-        public JSONXmlReader(JSONParser parser) {
+        readonly string m_rootName;
+        readonly string m_prefix;
+        readonly string m_namespaceUri;
+        readonly bool m_flattenArrays;
+        readonly string m_arrayItemName;
+        readonly XmlNameTable m_nameTable;
+        
+        public JSONXmlReader(JSONParser parser, JSONXmlReaderOptions options) {
             Safe.ArgumentNotNull(parser, "parser");
             m_parser = parser;
+
+            if (options != null) {
+                m_prefix = options.NodesPrefix ?? String.Empty;
+                m_namespaceUri = options.NamespaceURI ?? String.Empty;
+                m_rootName = options.RootName ?? "json";
+                m_flattenArrays = options.FlattenArrays;
+                m_arrayItemName = options.ArrayItemName ?? "item";
+                m_nameTable = options.NameTable ?? new NameTable();
+            } else {
+                m_prefix = String.Empty;
+                m_namespaceUri = String.Empty;
+                m_rootName = "json";
+                m_flattenArrays = false;
+                m_arrayItemName = "item";
+                m_nameTable = new NameTable();
+            }
         }
 
         /// <summary>
@@ -194,7 +212,7 @@
                         RestoreLocalName();
                         break;
                 }
-                string itemName = m_parser.ElementType == JSONElementType.None ? m_rootName : m_flattenArrays ? m_localName.localName : "item";
+                string itemName = m_parser.ElementType == JSONElementType.None ? m_rootName : m_flattenArrays ? m_localName.localName : m_arrayItemName;
                 while (m_parser.Read()) {
                     if (!String.IsNullOrEmpty(m_parser.ElementName))
                         itemName = m_parser.ElementName;
@@ -282,5 +300,11 @@
             base.Dispose(disposing);
         }
 
+
+        public static JSONXmlReader OpenFile(string file, JSONXmlReaderOptions options) {
+            var stream = File.OpenText(file);
+            var parser = new JSONParser(stream, true);
+            return new JSONXmlReader(parser, options);
+        }
     }
 }