Documentation > CMS Template API Library > DataContractSerializer > SerializeJson(Object)

SerializeJson

WARNING Experimental. It could change in a future release. Use at your own risk. Define a class that uses [DataContract], [DataMember], and [EnumMember] tags and you can then serialize an instance of that class to JSON with this method Use the Name property in the DataMember to use a name that is different from the property name -- [DataContract(Name="MySpecialName")] Use the Namespace in the DataContract to use a namespace that is custom for XML -- [DataContract(Namespace = "MySpecialNamespace")]

public System.String SerializeJson(Object)

Parameters

NameDescriptionType
obj System.Object

Code Example

C#

Sample:
        private static string ReturnSerializedData(DataType enumDataType, object SerializeData)
        {
            string szRet = string.Empty;

            switch (enumDataType)
            {
                case DataType.JSON:
                    szRet = CrownPeak.CMSAPI.Experimental.DataContractSerializer.SerializeJson(SerializeData);
                    break;

                case DataType.XML:
                    szRet = CrownPeak.CMSAPI.Experimental.DataContractSerializer.SerializeXml(SerializeData);
                    break;
            }

            return szRet;
        }

        public static string SerializeAssets(string szRootFolder, DataType enumDataType)
        {
            var SerializeData = new AssetObject();
            SerializeData.Assets = new List<AssetInfo>();

            FilterParams fpFilter = new FilterParams();
            fpFilter.Add(Comparison.Equals, AssetType.File);
            foreach (Asset aFile in Asset.Load(szRootFolder).GetFilterList(fpFilter))
            {
                Dictionary<string, string> dtTags = new Dictionary<string, string>();
                foreach (KeyValuePair<string, string> kvpContent in aFile.GetContent())
                {
                    if (kvpContent.Key.Contains("_tag:") && !string.IsNullOrWhiteSpace(kvpContent.Value))
                    {
                        if (!dtTags.ContainsKey(kvpContent.Key))
                            dtTags.Add(kvpContent.Key, kvpContent.Value);
                    }
                }

                if (dtTags.Count > 0)
                    SerializeData.Assets.Add(new AssetInfo { AssetId = aFile.Id, AssetLabel = aFile.Label, Tags = dtTags });
            }

            return ReturnSerializedData(enumDataType, SerializeData);
        }

Connect with Crownpeak