Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Get With the Times - Get Rid Of the Bloated XML Data Format And Switch To JSON

DZone's Guide to

Get With the Times - Get Rid Of the Bloated XML Data Format And Switch To JSON

· Java Zone ·
Free Resource

Get the Edge with a Professional Java IDE. 30-day free trial.

  1. One of my blog readers told me it was 2014

  2. He laughed at the fact that I was still using XML

  3. So within 30 minutes I was writing JSON to disk, instead of XML

  4. It was incredibly easy to do

  5. It is much easier than you might expect

  6. I eliminated an un-needed class and persisted my data into a smaller payload

INSTALL THE NUGET PACKAGE FOR JSON.NET

  1. In Visual Studio use the Tools menu

    image001

    Figure 1: Adding a NuGet Package - Json.Net

THE OLD XML-BASED APPROACH

  1. This is the code that I eliminated to Json-ify it

    • Line 15 and 50 were changed to make it Json
    • Lines 71 to 104 were eliminated

Old XML Code:

/////////////////////////////////////////////////////////////////////////////////////////////////////////////
//  Old Code using XML
//
//
private static async Task SaveToDiskFolders(ObservableCollection<FoldersItem> collection, string filename)
{
    // Creat a save-able object list
    List<FoldersItemDisk> dataToSave = new List<FoldersItemDisk>();
    foreach (FoldersItem item in collection)
    {
        dataToSave.Add(new FoldersItemDisk { FolderName = item.FolderName });
    }

    // Make xml out of it
    string localData = ObjectSerializer<List<FoldersItemDisk>>.ToXml(dataToSave);

    // Save to disk part 1
    StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
    StorageFile storageFile =
            await storageFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);

    // Save to disk part 2
    using (IRandomAccessStream stream = await storageFile.OpenAsync(FileAccessMode.ReadWrite))
    {
        using (DataWriter dataWriter = new DataWriter(stream))
        {
            dataWriter.WriteString(localData);
            await dataWriter.StoreAsync();
        }
    }
}



private async Task ReadFromFileFolders(ObservableCollection<FoldersItem> collection, ResetCollectionWithSource myfunc, string filename)
{
    try
    {
        StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
        StorageFile storageFile =
                await storageFolder.GetFileAsync(filename);

        using (IRandomAccessStreamWithContentType readStream = await storageFile.OpenReadAsync())
        using (DataReader reader = new DataReader(readStream))
        {
            ulong streamSize = readStream.Size;
            UInt32 totalBytesRead = await reader.LoadAsync((UInt32)streamSize);

            string s = reader.ReadString(totalBytesRead);
            List<FoldersItemDisk> localData = ObjectSerializer<List<FoldersItemDisk>>.FromXml(s);
            collection.Clear();
            foreach (FoldersItemDisk item in localData)
            {
                collection.Add(new FoldersItem {
                        FolderName = item.FolderName,
                        Folder = await StorageFolder.GetFolderFromPathAsync(item.FolderName)
                });
            }
            myfunc();
           
        }
    }
    catch (FileNotFoundException)
    {

    }

}


internal static class ObjectSerializer<T>
{
        // Serialize to xml
        public static string ToXml(T value)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            StringBuilder stringBuilder = new StringBuilder();
            XmlWriterSettings settings = new XmlWriterSettings()
            {
                Indent = true,
                OmitXmlDeclaration = true,
            };

            using (XmlWriter xmlWriter = XmlWriter.Create(stringBuilder, settings))
            {
                serializer.Serialize(xmlWriter, value);
            }
            return stringBuilder.ToString();
        }

        // Deserialize from xml
        public static T FromXml(string xml)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            T value;
            using (StringReader stringReader = new StringReader(xml))
            {
                object deserialized = serializer.Deserialize(stringReader);
                value = (T)deserialized;
            }

            return value;
        }
} 

THE NEW, MODERN, JSON-BASED APPROACH

  1. After adding the Json.NET package, I just changed the code to look like this

    • Lines 15 to 18 are new
    • Lines 54 to 57 are new
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
//  New Code using JSON
//
//
private static async Task SaveToDiskFolders(ObservableCollection<FoldersItem> collection, string filename)
{
    // Creat a save-able object list
    List<FoldersItemDisk> dataToSave = new List<FoldersItemDisk>();
    foreach (FoldersItem item in collection)
    {
        dataToSave.Add(new FoldersItemDisk { FolderName = item.FolderName });
    }

    // New JSON approach
    string localData = JsonConvert.SerializeObject(dataToSave, new JsonSerializerSettings
    {
        Formatting = Formatting.Indented
    });


    // Save to disk part 1
    StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
    StorageFile storageFile =
            await storageFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);

    // Save to disk part 2
    using (IRandomAccessStream stream = await storageFile.OpenAsync(FileAccessMode.ReadWrite))
    {
        using (DataWriter dataWriter = new DataWriter(stream))
        {
            dataWriter.WriteString(localData);
            await dataWriter.StoreAsync();
        }
    }
}



private async Task ReadFromFileFolders(ObservableCollection<FoldersItem> collection, ResetCollectionWithSource myfunc, string filename)
{
    try
    {
        StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
        StorageFile storageFile =
                await storageFolder.GetFileAsync(filename);

        using (IRandomAccessStreamWithContentType readStream = await storageFile.OpenReadAsync())
        using (DataReader reader = new DataReader(readStream))
        {
            ulong streamSize = readStream.Size;
            UInt32 totalBytesRead = await reader.LoadAsync((UInt32)streamSize);

            string s = reader.ReadString(totalBytesRead);
            List<FoldersItemDisk> localData = JsonConvert.DeserializeObject<List<FoldersItemDisk>>(s, new JsonSerializerSettings
            {
                Formatting = Formatting.Indented
            });
            collection.Clear();
            foreach (FoldersItemDisk item in localData)
            {
                collection.Add(new FoldersItem {
                        FolderName = item.FolderName,
                        Folder = await StorageFolder.GetFolderFromPathAsync(item.FolderName)
                });
            }
            myfunc();
           
        }
    }
    catch (FileNotFoundException)
    {

    }

} 

Conclusion

Now is the time to go to your code and bring it into 2014.

Get the Java IDE that understands code & makes developing enjoyable. Level up your code with IntelliJ IDEA. Download the free trial.

Topics:

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}