@@ -1671,6 +1671,68 @@ internal static string CreateJsonObject(IEnumerable<KeyValuePair<string, string>
16711671 return builder . ToString ( ) ;
16721672 }
16731673
1674+ /// <summary>
1675+ /// Serializes the given message as a JSON string.
1676+ /// </summary>
1677+ /// <param name="message">The message to serialize.</param>
1678+ /// <param name="messageDescriptions">The cached message descriptions to use for reflection.</param>
1679+ /// <returns>A JSON string.</returns>
1680+ [ SuppressMessage ( "Microsoft.Usage" , "CA2202:Do not dispose objects multiple times" , Justification = "This Dispose is safe." ) ]
1681+ [ SuppressMessage ( "Microsoft.Reliability" , "CA2000:Dispose objects before losing scope" , Justification = "No apparent problem. False positive?" ) ]
1682+ internal static string SerializeAsJson ( IMessage message , MessageDescriptionCollection messageDescriptions ) {
1683+ Requires . NotNull ( message , "message" ) ;
1684+ Requires . NotNull ( messageDescriptions , "messageDescriptions" ) ;
1685+
1686+ var encoding = Encoding . UTF8 ;
1687+ var bytes = SerializeAsJsonBytes ( message , messageDescriptions , encoding ) ;
1688+ string json = encoding . GetString ( bytes ) ;
1689+ return json ;
1690+ }
1691+
1692+ /// <summary>
1693+ /// Serializes the given message as a JSON string.
1694+ /// </summary>
1695+ /// <param name="message">The message to serialize.</param>
1696+ /// <param name="messageDescriptions">The cached message descriptions to use for reflection.</param>
1697+ /// <param name="encoding">The encoding to use. Defaults to <see cref="Encoding.UTF8"/></param>
1698+ /// <returns>A JSON string.</returns>
1699+ [ SuppressMessage ( "Microsoft.Usage" , "CA2202:Do not dispose objects multiple times" , Justification = "This Dispose is safe." ) ]
1700+ [ SuppressMessage ( "Microsoft.Reliability" , "CA2000:Dispose objects before losing scope" , Justification = "No apparent problem. False positive?" ) ]
1701+ internal static byte [ ] SerializeAsJsonBytes ( IMessage message , MessageDescriptionCollection messageDescriptions , Encoding encoding = null ) {
1702+ Requires . NotNull ( message , "message" ) ;
1703+ Requires . NotNull ( messageDescriptions , "messageDescriptions" ) ;
1704+
1705+ encoding = encoding ?? Encoding . UTF8 ;
1706+ MessageDictionary messageDictionary = messageDescriptions . GetAccessor ( message ) ;
1707+ using ( var memoryStream = new MemoryStream ( ) ) {
1708+ using ( var jsonWriter = JsonReaderWriterFactory . CreateJsonWriter ( memoryStream , encoding ) ) {
1709+ MessageSerializer . Serialize ( messageDictionary , jsonWriter ) ;
1710+ jsonWriter . Flush ( ) ;
1711+ }
1712+
1713+ return memoryStream . ToArray ( ) ;
1714+ }
1715+ }
1716+
1717+ /// <summary>
1718+ /// Deserializes a JSON object into a message.
1719+ /// </summary>
1720+ /// <param name="jsonBytes">The buffer containing the JSON string.</param>
1721+ /// <param name="receivingMessage">The message to deserialize the object into.</param>
1722+ /// <param name="messageDescriptions">The cache of message descriptions.</param>
1723+ /// <param name="encoding">The encoding that the JSON bytes are in.</param>
1724+ internal static void DeserializeFromJson ( byte [ ] jsonBytes , IMessage receivingMessage , MessageDescriptionCollection messageDescriptions , Encoding encoding = null ) {
1725+ Requires . NotNull ( jsonBytes , "jsonBytes" ) ;
1726+ Requires . NotNull ( receivingMessage , "receivingMessage" ) ;
1727+ Requires . NotNull ( messageDescriptions , "messageDescriptions" ) ;
1728+
1729+ encoding = encoding ?? Encoding . UTF8 ;
1730+ MessageDictionary messageDictionary = messageDescriptions . GetAccessor ( receivingMessage ) ;
1731+ using ( var jsonReader = JsonReaderWriterFactory . CreateJsonReader ( jsonBytes , 0 , jsonBytes . Length , encoding , Channel . DefaultUntrustedXmlDictionaryReaderQuotas , null ) ) {
1732+ MessageSerializer . Deserialize ( messageDictionary , jsonReader ) ;
1733+ }
1734+ }
1735+
16741736 /// <summary>
16751737 /// Prepares what SHOULD be simply a string value for safe injection into Javascript
16761738 /// by using appropriate character escaping.
0 commit comments