comparison Implab/JSON/JSONXmlReader.cs @ 64:a809805210d1

small refactoring
author cin
date Wed, 25 Jun 2014 04:44:53 +0400
parents 908b4f340c69
children 05f74c39a143
comparison
equal deleted inserted replaced
63:908b4f340c69 64:a809805210d1
36 readonly string m_prefix; 36 readonly string m_prefix;
37 readonly string m_namespaceUri; 37 readonly string m_namespaceUri;
38 readonly bool m_flattenArrays; 38 readonly bool m_flattenArrays;
39 readonly string m_arrayItemName; 39 readonly string m_arrayItemName;
40 readonly XmlNameTable m_nameTable; 40 readonly XmlNameTable m_nameTable;
41 readonly bool m_disposeParser; 41
42 42 JSONXmlReader(JSONParser parser, JSONXmlReaderOptions options) {
43 public JSONXmlReader(JSONParser parser, JSONXmlReaderOptions options) {
44 Safe.ArgumentNotNull(parser, "parser");
45 m_parser = parser; 43 m_parser = parser;
46 44
47 if (options != null) { 45 if (options != null) {
48 m_prefix = options.NodesPrefix ?? String.Empty; 46 m_prefix = options.NodesPrefix ?? String.Empty;
49 m_namespaceUri = options.NamespaceURI ?? String.Empty; 47 m_namespaceUri = options.NamespaceURI ?? String.Empty;
50 m_rootName = options.RootName ?? "json"; 48 m_rootName = options.RootName ?? "json";
51 m_flattenArrays = options.FlattenArrays; 49 m_flattenArrays = options.FlattenArrays;
52 m_arrayItemName = options.ArrayItemName ?? "item"; 50 m_arrayItemName = options.ArrayItemName ?? "item";
53 m_nameTable = options.NameTable ?? new NameTable(); 51 m_nameTable = options.NameTable ?? new NameTable();
54 m_disposeParser = options.DisposeParser;
55 } else { 52 } else {
56 m_prefix = String.Empty; 53 m_prefix = String.Empty;
57 m_namespaceUri = String.Empty; 54 m_namespaceUri = String.Empty;
58 m_rootName = "json"; 55 m_rootName = "json";
59 m_flattenArrays = false; 56 m_flattenArrays = false;
60 m_arrayItemName = "item"; 57 m_arrayItemName = "item";
61 m_nameTable = new NameTable(); 58 m_nameTable = new NameTable();
62 m_disposeParser = false;
63 } 59 }
64 } 60 }
65 61
66 /// <summary> 62 /// <summary>
67 /// Always 0, JSON doesn't support attributes 63 /// Always 0, JSON doesn't support attributes
304 300
305 } 301 }
306 302
307 protected override void Dispose(bool disposing) { 303 protected override void Dispose(bool disposing) {
308 if (disposing) { 304 if (disposing) {
309 if (m_disposeParser) 305 m_parser.Dispose();
310 m_parser.Dispose();
311 } 306 }
312 base.Dispose(disposing); 307 base.Dispose(disposing);
313 } 308 }
314 309
315 310 public static JSONXmlReader Create(string file, JSONXmlReaderOptions options) {
316 public static JSONXmlReader OpenFile(string file, JSONXmlReaderOptions options) { 311 return Create(File.OpenText(file), options);
317 var stream = File.OpenText(file); 312 }
318 var parser = new JSONParser(stream, true); 313
319 return new JSONXmlReader(parser, options); 314 public static JSONXmlReader Create(TextReader reader, JSONXmlReaderOptions options) {
315 return new JSONXmlReader(new JSONParser(reader, true), options);
316 }
317
318 public static JSONXmlReader Create(Stream stream, JSONXmlReaderOptions options) {
319 Safe.ArgumentNotNull(stream, "stream");
320 // HACK don't dispose StreaReader to keep stream opened
321 return Create(new StreamReader(stream), options);
320 } 322 }
321 } 323 }
322 } 324 }