Skip to content
Permalink
Browse files

Fix JSV encoding to always encode strings

  • Loading branch information...
mythz committed Aug 3, 2019
1 parent b07bd02 commit 04bd32f2b6f796b188334ed0d12468d7552fde8f
@@ -126,8 +126,8 @@ public static string SerializeToString<T>(T value)
public static string SerializeToString(object value, Type type)
{
if (value == null) return null;
if (type == typeof(string))
return value as string;
if (value is string str)
return str.EncodeJsv();

var writer = StringWriterThreadStatic.Allocate();
JsvWriter.GetWriteFn(type)(writer, value);
@@ -137,9 +137,9 @@ public static string SerializeToString(object value, Type type)
public static void SerializeToWriter<T>(T value, TextWriter writer)
{
if (value == null) return;
if (typeof(T) == typeof(string))
if (value is string str)
{
writer.Write(value);
writer.Write(str.EncodeJsv());
}
else if (typeof(T) == typeof(object))
{
@@ -160,9 +160,9 @@ public static void SerializeToWriter<T>(T value, TextWriter writer)
public static void SerializeToWriter(object value, Type type, TextWriter writer)
{
if (value == null) return;
if (type == typeof(string))
if (value is string str)
{
writer.Write(value);
writer.Write(str.EncodeJsv());
return;
}

@@ -305,8 +305,7 @@ public static void Print(this long longValue)

public static string SerializeAndFormat<T>(this T instance)
{
var fn = instance as Delegate;
if (fn != null)
if (instance is Delegate fn)
return Dump(fn);

var dtoStr = !HasCircularReferences(instance)
@@ -102,5 +102,14 @@ public void Can_serialize_ModelWithNullableFloatTypes_From_String()
fromJsv = jsv.FromJsv<ModelWithNullableFloatTypes>();
Assert.That(fromJsv, Is.EqualTo(dto));
}

[Test]
public void Does_encode_object_string_values_with_escaped_chars()
{
var url = "https://url.com";
Assert.That(url.ToJsv(), Is.EqualTo("\"https://url.com\""));
Assert.That(((object)url).ToJsv(), Is.EqualTo("\"https://url.com\""));
}

}
}

0 comments on commit 04bd32f

Please sign in to comment.
You can’t perform that action at this time.