Answered by:
C++/CX JSON Serialization

Question
-
Hi C++/CX experts,
Am a .NET developer and new to C++/CX. Am trying to port C# Windows 8 App to C++/CX. Can you please help with C++/CX JSON serialization?
Seeking expert help on C++/CX JSON serialization and templates in C++ /CX for porting from C# Windows 8 App to C++ Windows 8 App.
The issue I am referring to is about that as DataContractJsonSerializer is a .NET dependency, how do you do without it? Can you please help stringify in C++/CX as in the following?
Here's the C# Code Snippet for JSON:
using System.IO;
using System.Runtime.Serialization.Json;
using System.Text;
using Windows.Data.Json;namespace PP
{
/// <summary>
/// A utility for serializing/deserializing JSON encoded datastructures. Many methods on this object depend on
/// DataContractJsonSerializer, so types may need to be annotated to express serialization/deserialization intent.
/// </summary>
internal static class JSON
{/// <summary>
/// Converts an object of type T to a JSON string representation.
/// </summary>
/// <typeparam name="T">The type of the object to be converted.</typeparam>
/// <param name="obj">The object to be converted to a JSON string.</param>
/// <param name="str">The resulting JSON string representation of the provided object. If conversion fails, the resulting value is the default string value.</param>
/// <returns>true if obj was converted to a JSON string successfully; otherwise, false.</returns>
internal static bool TryStringify<T>(T obj, out string str)
{
str = default(string);
try
{
str = Stringify<T>(obj);
}
catch
{
return false;
}
return true;
}
/// <summary>
/// Converts an object of type T to a JSON string representation.
/// </summary>
/// <typeparam name="T">The type of the object to be converted.</typeparam>
/// <param name="obj">The object to be converted to a JSON string.</param>
/// <returns>The resulting JSON string representation of the provided object.</returns>
internal static string Stringify<T>(T obj)
{
MemoryStream stream = new MemoryStream();
DataContractJsonSerializerSettings settings = new DataContractJsonSerializerSettings();
settings.UseSimpleDictionaryFormat = true;DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T), settings);
serializer.WriteObject(stream, obj);
stream.Position = 0;string result = "";
using (StreamReader streamReader = new StreamReader(stream))
{
result = streamReader.ReadToEnd();
}return result;
}
/// <summary>
/// Converts the contents of a stream to a JsonObject
/// </summary>
/// <param name="stream">The stream to be converted to a JsonObject.</param>
/// <param name="obj">The JsonObject resulting from successful parsing. If parsing fails, the resulting value is the default JsonObject value.</param>
/// <returns>true if the stream was parsed successfully; otherwise, false</returns>
internal static bool TryParse(Stream stream, out JsonObject obj)
{
obj = default(JsonObject);
try {
using (StreamReader reader = new StreamReader(stream))
{
string json = reader.ReadToEnd();
stream.Position = 0;
return TryParse(json, out obj);
}
}
catch
{
return false;
}
}
/// <summary>
/// Converts the provided JSON formatted string to a JsonObject
/// </summary>
/// <param name="str">The string to be converted to a JsonObject.</param>
/// <param name="obj">The JsonObject resulting from successful parsing. If parsing fails, the resulting value is the default JsonObject value.</param>
/// <returns>true if the string was parsed successfully; otherwise, false</returns>
internal static bool TryParse(string str, out JsonObject obj)
{
return JsonObject.TryParse(str, out obj);
}
/// <summary>
/// Converts the provided JSON formatted string to an object of type T
/// </summary>
/// <typeparam name="T">The type of the resulting object.</typeparam>
/// <param name="str">The JSON formatted string to be converted to the provided type.</param>
/// <param name="obj">The object of type T resulting from successful parsing. If parsing fails, the resulting value is the default T value.</param>
/// <returns>true if the string was parsed successfully; otherwise, false</returns>
internal static bool TryParse<T>(string str, out T obj)
{
obj = default(T);
try
{
obj = Parse<T>(str);
}
catch
{
return false;
}
return true;
}
/// <summary>
/// Converts the provided stream to an object of type T
/// </summary>
/// <typeparam name="T">The type of the resulting object.</typeparam>
/// <param name="stream">The stream to be converted to an object of type T.</param>
/// <param name="obj">The object of type T resulting from successful parsing. If parsing fails, the resulting value is the default T value.</param>
/// <returns>true if the stream was parsed successfully; otherwise, false</returns>
internal static bool TryParse<T>(Stream stream, out T obj)
{
obj = default(T);
try
{
obj = Parse<T>(stream);
}
catch
{
return false;
}
return true;
}
/// <summary>
/// Converts the provided JSON formatted string to an object of type T.
/// </summary>
/// <typeparam name="T">The type of the resulting object.</typeparam>
/// <param name="str">The JSON formatted string to be converted to an object of type T.</param>
/// <returns>If parsing is successful, the resulting object of type T created; otherwise the default value for type T.</returns>
internal static T Parse<T>(string str)
{
T result = default(T);
using (MemoryStream stream = new MemoryStream(Encoding.Unicode.GetBytes(str)))
{
result = Parse<T>(stream);
}
return result;
}/// <summary>
/// Converts the provided stream to an object of type T.
/// </summary>
/// <typeparam name="T">Type type of the resulting object.</typeparam>
/// <param name="stream">The stream to be converted to an object of type T.</param>
/// <returns>If parsing is successful, the resulting object of type T created; otherwise the default value for type T.</returns>
internal static T Parse<T>(Stream stream)
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
return (T)serializer.ReadObject(stream);
}
}
}Since DataContractJsonSerializer is a .NET dependency, can you please help with C++/CX JSON utility class, similar along the following lines:
http://casablanca.codeplex.com/wikipage?title=JSON&referringTitle=Documentation
http://casablanca.codeplex.com/
Thanks
- Edited by recherche Thursday, May 16, 2013 7:45 AM typo
Thursday, May 16, 2013 7:43 AM
Answers
-
You can use any standard C++ JSON library like http://jsoncpp.sourceforge.net/
- Marked as answer by recherche Saturday, May 18, 2013 5:25 PM
Thursday, May 16, 2013 1:18 PM
All replies
-
You can use any standard C++ JSON library like http://jsoncpp.sourceforge.net/
- Marked as answer by recherche Saturday, May 18, 2013 5:25 PM
Thursday, May 16, 2013 1:18 PM -
If you want to use pure WinRT API and avoid 3rd party libraries, see this blog entry :
http://sridharpoduri.com/2012/07/02/using-json-and-c-in-windows-8-apps/
Thursday, May 16, 2013 1:19 PM -
You can use any standard C++ JSON library like http://jsoncpp.sourceforge.net/
Thanks for the replySaturday, May 18, 2013 5:25 PM