public object Convert(int operation, string packet) { object _packet = null; CharacterOperation key = (CharacterOperation)operation; JsonReaderSettings settings = new JsonReaderSettings(); settings.AddTypeConverter (new VectorConverter()); settings.AddTypeConverter (new QuaternionConverter()); JsonReader jsonReader = new JsonReader(packet, settings); switch(key) { case CharacterOperation.GROUNDCLICK: _packet = jsonReader.Deserialize<GroundClick>(); break; case CharacterOperation.INSTANTIATE: _packet = jsonReader.Deserialize<InstantiateChar>(); break; case CharacterOperation.DESTROY: _packet = jsonReader.Deserialize<DestroyChar>(); break; default: break; } return _packet; }
void PrintResults(string rawJson) { // Raw output: /*DB.Log(DC.Log("******** raw string from Twitter ********")); * DB.Log(DC.Log(rawJson)); * * * // Turn the JSON into C# objects * var search = JsonReader.Deserialize<TwitterSearchResults>(rawJson); * * * // iterate through the array of results; * DB.Log(DC.Log("******** search results ********")); * * * foreach (var tweet in search.results) { * DB.Log(DC.Log(tweet.from_user_name + " : " + tweet.text)); * } * * DB.Log(DC.Log("******** serialize an entity ********")); * * JsonWriterSettings settings = new JsonWriterSettings(); * settings.PrettyPrint = true; * * System.Text.StringBuilder output = new System.Text.StringBuilder(); * * JsonWriter writer = new JsonWriter (output,settings); * writer.Write (search.results[0]); * * // this turns a C# object into a JSON string. * string json = output.ToString();//JsonWriter.Serialize(); * * DB.Log(DC.Log(json));*/ for (int i = 0; i < 10; i++) { System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch(); watch.Start(); System.Text.StringBuilder output = new System.Text.StringBuilder(); Debug.Log("+++ Serializing +++"); JsonWriterSettings settings = new JsonWriterSettings(); settings.PrettyPrint = false; settings.AddTypeConverter(new VectorConverter()); TestClass test = new TestClass(); test.vec.y = 128.513589999F; JsonWriter writer = new JsonWriter(output, settings); Debug.Log("+++ Writing +++"); writer.Write(test); if (i == 0) { Debug.Log(output.ToString()); } Debug.Log("+++ Deserializing - Init +++"); JsonReaderSettings settings2 = new JsonReaderSettings(); settings2.AddTypeConverter(new VectorConverter()); JsonReader reader = new JsonReader(output.ToString(), settings2); Debug.Log("+++ Deserializing +++"); TestClass deserialized = reader.Deserialize <TestClass>(); watch.Stop(); Debug.Log((watch.ElapsedTicks * 0.0001).ToString("0.00")); Debug.Log(deserialized.vec.y.ToString("r")); } }
public static void RunTest(TextWriter writer, string unitTestsFolder, string outputFolder) { string[] unitTests = Directory.GetFiles(unitTestsFolder, UnitTestsFiles, SearchOption.AllDirectories); if (unitTests.Length > 0) { JsonReaderSettings readerSettings = new JsonReaderSettings(); readerSettings.TypeHintName = StronglyTyped.MyTypeHintName; readerSettings.AllowNullValueTypes = true; readerSettings.AllowUnquotedObjectKeys = true; JsonWriterSettings writerSettings = new JsonWriterSettings(); writerSettings.TypeHintName = StronglyTyped.MyTypeHintName; writerSettings.PrettyPrint = false; writerSettings.MaxDepth = 100; writer.WriteLine(JsonText.Seperator); writer.WriteLine("JsonReaderSettings:"); new JsonWriter(writer).Write(readerSettings); writer.WriteLine(JsonText.Seperator); writer.WriteLine("JsonWriterSettings:"); new JsonWriter(writer).Write(writerSettings); foreach (string unitTest in unitTests) { string source = String.Empty; try { writer.WriteLine(JsonText.Seperator); source = File.ReadAllText(unitTest); JsonReader jsonReader = new JsonReader(source, readerSettings); object obj, obj2; obj2 = obj = jsonReader.Deserialize(); do { writer.WriteLine("READ: {0}", unitTest.Replace(unitTestsFolder, "")); writer.WriteLine("Result: {0}", (obj == null) ? "null" : obj.GetType().FullName); obj = jsonReader.Deserialize(); } while (obj != null); string outputFile = unitTest.Replace(unitTestsFolder, outputFolder); string outputDir = Path.GetDirectoryName(outputFile); if (!Directory.Exists(outputDir)) { Directory.CreateDirectory(outputDir); } using (JsonWriter jsonWriter = new JsonWriter(outputFile, writerSettings)) { jsonWriter.Write(obj2); } } catch (JsonDeserializationException ex) { int col, line; ex.GetLineAndColumn(source, out line, out col); writer.WriteLine("ERROR: {0}", unitTest.Replace(unitTestsFolder, "")); writer.WriteLine("-- \"{0}\" ({1}, {2})", ex.Message, line, col); continue; } catch (Exception ex) { writer.WriteLine("ERROR: {0}", unitTest.Replace(unitTestsFolder, "")); writer.WriteLine("-- \"{0}\"", ex.Message); continue; } } } else { writer.WriteLine(ErrorMessage); } }
public static void RunTest(TextWriter writer, string unitTestsFolder, string outputFolder) { JsonWriterSettings wsettings = new JsonWriterSettings(); wsettings.HandleCyclicReferences = true; wsettings.DebugMode = true; wsettings.PrettyPrint = true; A a = new A(); a.b = new B(); a.b.a = a; a.a = a; a.b.a2 = new A(); a.b.a2.a = a; a.b.b2 = new B(); a.b.b2.a = a.b.a2; a.b.b2.b2 = new B(); a.b.b2.b2.b2 = new B(); a.b.b2.b2.b2.b2 = new B(); a.b.b2.b2.b2.b2.b2 = new B(); a.b.b2.b2.b2.b2.b2.b2 = new B(); a.b.b2.b2.b2.b2.b2.b2.b2 = a.b.b2.b2; a.b.b2.b2.b2.b2.b2.b2.a = a; a.q[0] = a; a.b.a2.q = a.q; a.d = new Dictionary <string, A>(); a.d["blah"] = a; a.d["meh"] = a.b.a2; A[] arr = new A[4]; arr[0] = a; arr[1] = a; arr[2] = new A(); arr[3] = arr[2]; arr[2].a = a; arr[2].b = a.b; for (int i = 4; i < arr.Length; i++) { arr[i] = new A(); } IEnumerator <int> co = TestRout(2); co.MoveNext(); Console.WriteLine(co.GetType()); Console.WriteLine(co.Current); co.MoveNext(); Console.WriteLine(co.Current); co.MoveNext(); Console.WriteLine(co.Current); co.MoveNext(); Console.WriteLine(co.Current); using (StreamWriter wr2 = new StreamWriter("out", false, Encoding.UTF8)) { JsonWriter wr = new JsonWriter(wr2, wsettings); //wr.Write(a); //wr.Write(arr); wr.Write(co); } using (StreamReader re = new StreamReader("out", Encoding.UTF8)) { JsonReaderSettings rsettings = new JsonReaderSettings(); rsettings.HandleCyclicReferences = true; /*JsonReader read = new JsonReader (re, rsettings); * a = (A)read.Deserialize(typeof(A)); * * // Do some checking * if (a == null || a.a != a || a.b.a != a || * a.b.a2.a != a || a.b.b2.a != a.b.a2 || * a.b.b2.b2.b2.b2.b2.b2.b2 != a.b.b2.b2 || * a.d["meh"] != a.b.a2 || a.d["blah"] != a) { * throw new System.Exception ("Invalid, could not deserialize or serialize cyclic classes correctly."); * } * * * object ob = read.Deserialize(typeof(A[])); * * arr = (A[])ob; * * if (arr[0] != a || arr[3] != arr[2]) { * throw new System.Exception ("Invalid, Could not serialize or deserialize array correctly"); * }*/ } //JsonReaderSettings rsettings = new JsonReaderSettings (); //rsettings. //JsonReader reader = new JsonReader (); }
public static void RunTest(TextWriter writer, string unitTestsFolder, string outputFolder) { var watch = System.Diagnostics.Stopwatch.StartNew(); JsonWriterSettings wsettings = new JsonWriterSettings(); wsettings.PrettyPrint = true; Root a = new Root(); a.ownedReference = new B(); // Self a.other = a; a.dictToOther = new Dictionary <string, Root>(); a.dictToOther["refToSelf"] = a; Root late = new Root(); late.other = a; late.dictToOther = new Dictionary <string, Root> (); late.dictToOther["refToFirst"] = a; late.dictToOther["refToFirst2"] = a; late.dictToOther["refToSelf"] = late; a.ownedReference.nestedRefToOther = late; a.listToOther = new List <Root> (); a.listToOther.Add(a); a.listToOther.Add(late); a.arrayToOther = new Root[] { a, late }; late.listToOther = new List <Root> (); late.listToOther.Add(a); late.listToOther.Add(late); late.arrayToOther = new Root[] { a, late }; for (int i = 0; i < 100; i++) { late.dictToOther [i.ToString()] = a; } var refHandler = new ReferenceHandlerWriter(); using (StreamWriter wr2 = new StreamWriter(outputFolder + "/References1", false, Encoding.UTF8)) { JsonWriter wr = new JsonWriter(wr2, wsettings); wr.referenceHandler = refHandler; wr.Write(a); } using (StreamWriter wr2 = new StreamWriter(outputFolder + "/References2", false, Encoding.UTF8)) { JsonWriter wr = new JsonWriter(wr2, wsettings); wr.referenceHandler = refHandler; wr.Write(late); } if (refHandler.GetNonSerializedReferences().Count != 0) { throw new System.Exception("Not all references were serialized"); } var refHandlerRead = new ReferenceHandlerReader(); using (StreamReader re = new StreamReader(outputFolder + "/References2", Encoding.UTF8)) { JsonReaderSettings rsettings = new JsonReaderSettings(); JsonReader read = new JsonReader(re, rsettings); read.referenceHandler = refHandlerRead; late = (Root)read.Deserialize(typeof(Root)); if (late.arrayToOther[0] != null) { throw new System.Exception("Array item should not have been deserialized yet"); } if (late.listToOther[0] != null) { throw new System.Exception("List item should not have been deserialized yet"); } } using (StreamReader re = new StreamReader(outputFolder + "/References1", Encoding.UTF8)) { JsonReaderSettings rsettings = new JsonReaderSettings(); JsonReader read = new JsonReader(re, rsettings); read.referenceHandler = refHandlerRead; a = (Root)read.Deserialize(typeof(Root)); // Do some checking if (a.other != a || a.ownedReference.nestedRefToOther != late || a.dictToOther ["refToSelf"] != a) { throw new System.Exception("Failed to serialize or deserialize references correctly"); } if (late.other != a) { throw new System.Exception("Failed to handle out of order references (case 1)"); } if (late.dictToOther ["refToSelf"] != late) { throw new System.Exception("Failed to handle out of order references (case 2)"); } if (late.dictToOther ["refToFirst"] != a || late.dictToOther ["refToFirst2"] != a) { throw new System.Exception("Failed to handle out of order references (case 3)"); } if (late.listToOther[0] != a || late.listToOther[1] != late || a.listToOther[0] != a || late.listToOther[1] != late) { throw new System.Exception("Failed to handle lists"); } if (late.arrayToOther[0] != a || late.arrayToOther[1] != late || a.arrayToOther[0] != a || late.arrayToOther[1] != late) { throw new System.Exception("Failed to handle arrays"); } } watch.Stop(); //System.Console.WriteLine (watch.Elapsed.TotalMilliseconds.ToString ("0.00")); }
public JsonReader(StringBuilder input, JsonReaderSettings settings) { Settings = settings; Source = input.ToString(); SourceLength = Source.Length; }
public JsonReader(string input, JsonReaderSettings settings) { Settings = settings; Source = input; SourceLength = Source.Length; }
public JsonReader(TextReader input, JsonReaderSettings settings) { Settings = settings; Source = input.ReadToEnd(); SourceLength = Source.Length; }