-Added generic JsonConvert.SerializeObject methods
-Fixed JToken.ToObject not using StringEnumConverter on enums
This commit is contained in:
@@ -133,7 +133,7 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
[Test]
|
||||
public void SerializeNullRoot()
|
||||
{
|
||||
string json = JsonConvert.SerializeObject(null, typeof(DataSet), new JsonSerializerSettings
|
||||
string json = JsonConvert.SerializeObject<DataSet>(null, new JsonSerializerSettings
|
||||
{
|
||||
Formatting = Formatting.Indented
|
||||
});
|
||||
|
||||
@@ -481,7 +481,7 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
[Test]
|
||||
public void SerializeNullRoot()
|
||||
{
|
||||
string json = JsonConvert.SerializeObject(null, typeof(DataTable), new JsonSerializerSettings
|
||||
string json = JsonConvert.SerializeObject<DataTable>(null, new JsonSerializerSettings
|
||||
{
|
||||
Formatting = Formatting.Indented
|
||||
});
|
||||
|
||||
@@ -45,6 +45,9 @@ using NUnit.Framework;
|
||||
#endif
|
||||
using Newtonsoft.Json;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Threading;
|
||||
#if NET20
|
||||
using Newtonsoft.Json.Utilities.LinqBridge;
|
||||
#else
|
||||
@@ -3452,6 +3455,73 @@ null//comment
|
||||
Assert.IsFalse(r.Read());
|
||||
}
|
||||
#endif
|
||||
|
||||
#if !DNXCORE50
|
||||
[Test]
|
||||
[Ignore]
|
||||
public void ReadFromNetworkStream()
|
||||
{
|
||||
const int port = 11999;
|
||||
const int jsonArrayElementsCount = 193;
|
||||
|
||||
var serverStartedEvent = new ManualResetEvent(false);
|
||||
var clientReceivedEvent = new ManualResetEvent(false);
|
||||
|
||||
#region server
|
||||
ThreadPool.QueueUserWorkItem(work =>
|
||||
{
|
||||
var server = new TcpListener(IPAddress.Parse("0.0.0.0"), port);
|
||||
server.Start();
|
||||
|
||||
serverStartedEvent.Set();
|
||||
|
||||
var serverSocket = server.AcceptSocket();
|
||||
|
||||
var jsonString = "[\r\n" + String.Join(",", Enumerable.Repeat(" \"testdata\"\r\n", jsonArrayElementsCount).ToArray()) + "]";
|
||||
var bytes = new UTF8Encoding().GetBytes(jsonString);
|
||||
serverSocket.Send(bytes);
|
||||
Console.WriteLine("server send: " + bytes.Length);
|
||||
|
||||
|
||||
clientReceivedEvent.WaitOne();
|
||||
|
||||
});
|
||||
#endregion
|
||||
|
||||
serverStartedEvent.WaitOne();
|
||||
|
||||
|
||||
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||
socket.Blocking = false;
|
||||
socket.Connect("127.0.0.1", port);
|
||||
|
||||
var stream = new NetworkStream(socket);
|
||||
var serializer = new JsonSerializer();
|
||||
|
||||
int i = 0;
|
||||
using (var sr = new StreamReader(stream, new UTF8Encoding(), false))
|
||||
using (var jsonTextReader = new JsonTextReader(sr))
|
||||
{
|
||||
while (jsonTextReader.Read())
|
||||
{
|
||||
i++;
|
||||
|
||||
if (i == 193)
|
||||
{
|
||||
string s = string.Empty;
|
||||
}
|
||||
|
||||
Console.WriteLine($"{i} - {jsonTextReader.TokenType} - {jsonTextReader.Value}");
|
||||
}
|
||||
//var result = serializer.Deserialize(jsonTextReader).ToString();
|
||||
//Console.WriteLine("client receive: " + new UTF8Encoding().GetBytes(result).Length);
|
||||
}
|
||||
|
||||
clientReceivedEvent.Set();
|
||||
|
||||
Console.WriteLine("Done");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
public class ToggleReaderError : TextReader
|
||||
|
||||
@@ -48,6 +48,7 @@ using Newtonsoft.Json.Utilities.LinqBridge;
|
||||
using System.Linq;
|
||||
#endif
|
||||
using System.IO;
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Linq
|
||||
{
|
||||
@@ -1329,6 +1330,57 @@ keyword such as type of business.""
|
||||
}
|
||||
#endif
|
||||
|
||||
#if !(NET20)
|
||||
[JsonConverter(typeof(StringEnumConverter))]
|
||||
public enum FooBar
|
||||
{
|
||||
[EnumMember(Value = "SOME_VALUE")]
|
||||
SomeValue,
|
||||
|
||||
[EnumMember(Value = "SOME_OTHER_VALUE")]
|
||||
SomeOtherValue
|
||||
}
|
||||
|
||||
public class MyObject
|
||||
{
|
||||
public FooBar FooBar { get; set; }
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ToObject_Enum_Converter()
|
||||
{
|
||||
JObject o = JObject.Parse("{'FooBar':'SOME_OTHER_VALUE'}");
|
||||
|
||||
FooBar e = o["FooBar"].ToObject<FooBar>();
|
||||
Assert.AreEqual(FooBar.SomeOtherValue, e);
|
||||
}
|
||||
|
||||
public enum FooBarNoEnum
|
||||
{
|
||||
[EnumMember(Value = "SOME_VALUE")]
|
||||
SomeValue,
|
||||
|
||||
[EnumMember(Value = "SOME_OTHER_VALUE")]
|
||||
SomeOtherValue
|
||||
}
|
||||
|
||||
public class MyObjectNoEnum
|
||||
{
|
||||
public FooBarNoEnum FooBarNoEnum { get; set; }
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ToObject_Enum_NoConverter()
|
||||
{
|
||||
JObject o = JObject.Parse("{'FooBarNoEnum':'SOME_OTHER_VALUE'}");
|
||||
|
||||
ExceptionAssert.Throws<ArgumentException>(() =>
|
||||
{
|
||||
o["FooBarNoEnum"].ToObject<FooBarNoEnum>();
|
||||
}, "Could not convert 'SOME_OTHER_VALUE' to FooBarNoEnum.");
|
||||
}
|
||||
#endif
|
||||
|
||||
[Test]
|
||||
public void SerializeWithNoRedundentIdPropertiesTest()
|
||||
{
|
||||
|
||||
@@ -245,6 +245,34 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
}", json);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SerializeRootTypeNameAutoWithJsonConvert_Generic()
|
||||
{
|
||||
string json = JsonConvert.SerializeObject<object>(new WagePerson(), Formatting.Indented, new JsonSerializerSettings
|
||||
{
|
||||
TypeNameHandling = TypeNameHandling.Auto
|
||||
});
|
||||
|
||||
StringAssert.AreEqual(@"{
|
||||
""$type"": ""Newtonsoft.Json.Tests.TestObjects.WagePerson, Newtonsoft.Json.Tests"",
|
||||
""HourlyWage"": 0.0,
|
||||
""Name"": null,
|
||||
""BirthDate"": ""0001-01-01T00:00:00"",
|
||||
""LastModified"": ""0001-01-01T00:00:00""
|
||||
}", json);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SerializeRootTypeNameAutoWithJsonConvert_Generic2()
|
||||
{
|
||||
string json = JsonConvert.SerializeObject<object>(new WagePerson(), new JsonSerializerSettings
|
||||
{
|
||||
TypeNameHandling = TypeNameHandling.Auto
|
||||
});
|
||||
|
||||
StringAssert.AreEqual(@"{""$type"":""Newtonsoft.Json.Tests.TestObjects.WagePerson, Newtonsoft.Json.Tests"",""HourlyWage"":0.0,""Name"":null,""BirthDate"":""0001-01-01T00:00:00"",""LastModified"":""0001-01-01T00:00:00""}", json);
|
||||
}
|
||||
|
||||
public class Wrapper
|
||||
{
|
||||
public IList<EmployeeReference> Array { get; set; }
|
||||
|
||||
Reference in New Issue
Block a user