/** * example from * http://msdn.microsoft.com/en-us/library/bb412179(v=vs.110).aspx * */ public void testDataMemberSimple() { Person p = new Person(); p.name = "John"; p.age = 42; p.title = "Mr"; MemoryStream stream1 = new MemoryStream(); DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Person)); ser.WriteObject(stream1, p); StreamReader sr = new StreamReader(stream1); stream1.Position = 0; string strBuiltInJson = sr.ReadToEnd(); //Console.WriteLine(sr.ReadToEnd()); Console.WriteLine("inbuilt json=" + strBuiltInJson); Object2Json o2j = new Object2Json(); o2j.NodeExpander = new DataContractFieldNodeExpander(); string strJson = o2j.toJson(p); System.Console.WriteLine("json:" + strJson); // analyze with JsonExplorer JSONExplorerImpl jsonExplorer = new JSONExplorerImpl(); TestListener inbuiltListener = new TestListener(); TestListener listener = new TestListener(); jsonExplorer.explore(strBuiltInJson, inbuiltListener); jsonExplorer.explore(strJson, listener); Assert.AreEqual(inbuiltListener.leaves.Keys.Count, listener.leaves.Keys.Count, "leaf count"); compareMaps(inbuiltListener.leaves, "inbuilt", listener.leaves, "local"); compareMaps(listener.leaves, "local", inbuiltListener.leaves, "inbuilt"); }
protected void testInBuiltAndNativeJson(Object o, JsonExpectationBlock jsonExpectation, string testDescription) { System.Console.WriteLine("starting " + testDescription + " ************"); MemoryStream stream1 = new MemoryStream(); DataContractJsonSerializer ser = new DataContractJsonSerializer(o.GetType()); ser.WriteObject(stream1, o); StreamReader sr = new StreamReader(stream1); stream1.Position = 0; string strBuiltInJson = sr.ReadToEnd(); var mocks = new MockRepository(); JsonExploreListener theMock = null; RhinoMocks.Logger = new TextWriterExpectationLogger(Console.Out); Object2Json o2j = new Object2Json(); o2j.NodeExpander = new DataContractFieldNodeExpander(); o2j.isDefaultLeafValue = DataContractDefaultUtil.isDefaultLeafValue; o2j.OmitDefaultLeafValuesInJs = true; string nativejson = o2j.toJson(o); JSONExplorerImpl jsonExplorerImpl = new JSONExplorerImpl(); string json = null; Func <string> runWithMocks = () => { theMock = mocks.StrictMock <JsonExploreListener>(); using (mocks.Ordered()) { jsonExpectation(theMock, json); } theMock.Replay(); jsonExplorerImpl.explore(json, theMock); theMock.VerifyAllExpectations(); return(null); }; Console.WriteLine("testing inbuilt json=" + strBuiltInJson); json = strBuiltInJson; runWithMocks(); Console.WriteLine("testing json=" + nativejson); json = nativejson; runWithMocks(); System.Console.WriteLine("completed " + testDescription + " ************"); }
public void testOrdering() { OrderingTestObject orderingTestObject = new OrderingTestObject(); orderingTestObject.four = "4"; orderingTestObject.three = "3"; orderingTestObject.one = "1"; orderingTestObject.two = "2"; var mocks = new MockRepository(); var theMock = mocks.StrictMock <JsonExploreListener>(); RhinoMocks.Logger = new TextWriterExpectationLogger(Console.Out); Object2Json o2j = new Object2Json(); o2j.NodeExpander = new DataContractFieldNodeExpander(); string json = o2j.toJson(orderingTestObject); System.Console.WriteLine("json:" + json); Console.WriteLine("***testArray json: " + json); using (mocks.Ordered()) { theMock.JsonStartObject(null, 0); theMock.JsonLeaf("one", "1", true); theMock.JsonLeaf("two", "2", true); theMock.JsonLeaf("three", "3", true); theMock.JsonLeaf("four", "4", true); theMock.JsonEndObject(json.Length - 1); } theMock.Replay(); JSONExplorerImpl jsonExplorerImpl = new JSONExplorerImpl(); jsonExplorerImpl.explore(json, theMock); theMock.VerifyAllExpectations(); }
/** * example from * http://msdn.microsoft.com/en-us/library/bb412179(v=vs.110).aspx * */ public void testDataMemberSimple() { Person p = new Person(); p.name = "John"; p.age = 42; p.title = "Mr"; MemoryStream stream1 = new MemoryStream(); DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Person)); ser.WriteObject(stream1, p); StreamReader sr = new StreamReader(stream1); stream1.Position=0; string strBuiltInJson = sr.ReadToEnd(); //Console.WriteLine(sr.ReadToEnd()); Console.WriteLine("inbuilt json=" + strBuiltInJson); Object2Json o2j = new Object2Json(); o2j.NodeExpander = new DataContractFieldNodeExpander(); string strJson = o2j.toJson(p); System.Console.WriteLine("json:" + strJson); // analyze with JsonExplorer JSONExplorerImpl jsonExplorer = new JSONExplorerImpl(); TestListener inbuiltListener = new TestListener(); TestListener listener = new TestListener(); jsonExplorer.explore(strBuiltInJson, inbuiltListener); jsonExplorer.explore(strJson, listener); Assert.AreEqual(inbuiltListener.leaves.Keys.Count, listener.leaves.Keys.Count, "leaf count"); compareMaps(inbuiltListener.leaves, "inbuilt", listener.leaves, "local"); compareMaps(listener.leaves, "local", inbuiltListener.leaves, "inbuilt"); }
protected void testInBuiltAndNativeJson(Object o, JsonExpectationBlock jsonExpectation, string testDescription) { System.Console.WriteLine("starting " + testDescription + " ************"); MemoryStream stream1 = new MemoryStream(); DataContractJsonSerializer ser = new DataContractJsonSerializer( o.GetType()); ser.WriteObject(stream1, o); StreamReader sr = new StreamReader(stream1); stream1.Position = 0; string strBuiltInJson = sr.ReadToEnd(); var mocks = new MockRepository(); JsonExploreListener theMock = null; RhinoMocks.Logger = new TextWriterExpectationLogger(Console.Out); Object2Json o2j = new Object2Json(); o2j.NodeExpander = new DataContractFieldNodeExpander(); o2j.isDefaultLeafValue = DataContractDefaultUtil.isDefaultLeafValue; o2j.OmitDefaultLeafValuesInJs = true; string nativejson = o2j.toJson(o); JSONExplorerImpl jsonExplorerImpl = new JSONExplorerImpl(); string json = null; Func<string> runWithMocks = () => { theMock = mocks.StrictMock<JsonExploreListener>(); using (mocks.Ordered()) { jsonExpectation(theMock, json); } theMock.Replay(); jsonExplorerImpl.explore(json, theMock); theMock.VerifyAllExpectations(); return null; }; Console.WriteLine("testing inbuilt json=" + strBuiltInJson); json = strBuiltInJson; runWithMocks(); Console.WriteLine("testing json=" + nativejson); json = nativejson; runWithMocks(); System.Console.WriteLine("completed " + testDescription + " ************"); }
public void testOrdering() { OrderingTestObject orderingTestObject = new OrderingTestObject(); orderingTestObject.four = "4"; orderingTestObject.three = "3"; orderingTestObject.one = "1"; orderingTestObject.two = "2"; var mocks = new MockRepository(); var theMock = mocks.StrictMock<JsonExploreListener>(); RhinoMocks.Logger = new TextWriterExpectationLogger(Console.Out); Object2Json o2j = new Object2Json(); o2j.NodeExpander = new DataContractFieldNodeExpander(); string json = o2j.toJson(orderingTestObject); System.Console.WriteLine("json:" + json); Console.WriteLine("***testArray json: " + json); using (mocks.Ordered()) { theMock.JsonStartObject(null, 0); theMock.JsonLeaf("one", "1", true); theMock.JsonLeaf("two", "2", true); theMock.JsonLeaf("three", "3", true); theMock.JsonLeaf("four", "4", true); theMock.JsonEndObject(json.Length - 1); } theMock.Replay(); JSONExplorerImpl jsonExplorerImpl = new JSONExplorerImpl(); jsonExplorerImpl.explore(json, theMock); theMock.VerifyAllExpectations(); }