FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Support string interning when deserializing string as object. by sa-vg · Pull Request #1487 · MessagePack-CSharp/MessagePack-CSharp · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .cs  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,8 @@ public object Deserialize(ref MessagePackReader reader, MessagePackSerializerOpt
}

case MessagePackType.String:
return reader.ReadString();
var stringFormatter = resolver.GetFormatter<string>();
return stringFormatter.Deserialize(ref reader, options);
case MessagePackType.Binary:
// We must copy the sequence returned by ReadBytes since the reader's sequence is only valid during deserialization.
return reader.ReadBytes()?.ToArray();
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,16 @@ public void EquivalentStringsGetSharedInstance(int length)
[Fact]
public void StringMemberInterning()
{
ClassOfStrings before = new ClassOfStrings { InternedString = "abc", OrdinaryString = "def" };
ClassOfStrings before = new ClassOfStrings { InternedString = "abc", OrdinaryString = "def", ObjectString = "obj" };
ClassOfStrings after1 = MessagePackSerializer.Deserialize<ClassOfStrings>(MessagePackSerializer.Serialize(before, MessagePackSerializerOptions.Standard), MessagePackSerializerOptions.Standard);
ClassOfStrings after2 = MessagePackSerializer.Deserialize<ClassOfStrings>(MessagePackSerializer.Serialize(before, MessagePackSerializerOptions.Standard), MessagePackSerializerOptions.Standard);
Assert.Equal(after1.InternedString, after2.InternedString);
Assert.Equal(after1.OrdinaryString, after2.OrdinaryString);
Assert.Equal(after1.ObjectString, after2.ObjectString);

Assert.Same(after1.InternedString, after2.InternedString);
Assert.NotSame(after1.OrdinaryString, after2.OrdinaryString);
Assert.NotSame(after1.ObjectString, after2.ObjectString);
}

[Fact]
Expand All @@ -83,14 +85,16 @@ public void StringMemberInterning_CustomResolver()
new IMessagePackFormatter[] { new StringInterningFormatter() },
new IFormatterResolver[] { StandardResolver.Instance }));

ClassOfStrings before = new ClassOfStrings { InternedString = "abc", OrdinaryString = "def" };
ClassOfStrings before = new ClassOfStrings { InternedString = "abc", OrdinaryString = "def", ObjectString = "obj" };
ClassOfStrings after1 = MessagePackSerializer.Deserialize<ClassOfStrings>(MessagePackSerializer.Serialize(before, options), options);
ClassOfStrings after2 = MessagePackSerializer.Deserialize<ClassOfStrings>(MessagePackSerializer.Serialize(before, options), options);
Assert.Equal(after1.InternedString, after2.InternedString);
Assert.Equal(after1.OrdinaryString, after2.OrdinaryString);
Assert.Equal(after1.ObjectString, after2.ObjectString);

Assert.Same(after1.InternedString, after2.InternedString);
Assert.Same(after1.OrdinaryString, after2.OrdinaryString);
Assert.Same(after1.ObjectString, after2.ObjectString);
}

[MessagePackObject]
Expand All @@ -102,6 +106,9 @@ public class ClassOfStrings

[Key(1)]
public string OrdinaryString { get; set; }

[Key(2)]
public object ObjectString { get; set; }
}
}
}

Back | FazBrowse Home | New Git URL