Did it work in .net6?
I don't think that if you serialize this way the serializer will use our registered converters and options.
You should resolve our options with converters and use them:
var options = ServiceLocator.Current.GetInstance<SystemTextJsonSettingsOptions>();
var jsonSerializerOptions = options.SerializerOptions.JsonSerializerOptions;
//your custom settings
jsonSerializerOptions.WriteIndented = true;
//your custom settings
var jsonString = JsonSerializer.Serialize(content, options.SerializerOptions.JsonSerializerOptions);
But I'm not sure if that way all your properties will be properly serialized.
I would advice to use this little class:
[ServiceConfiguration]
public class SimpleIContentSerializer
{
private readonly IContentStoreModelCreator _contentStoreModelCreator;
private readonly IObjectSerializer _objectSerializer;
public SimpleIContentSerializer(IContentStoreModelCreator contentStoreModelCreator, IObjectSerializer objectSerializer)
{
_contentStoreModelCreator = contentStoreModelCreator;
_objectSerializer = objectSerializer;
}
public string Serialize(IContent content)
{
var queryParameters = new DefaultQueryParameters
{
TypeIdentifiers = Enumerable.Empty<string>(),
AllParameters = new NameValueCollection()
};
var creator = _contentStoreModelCreator.CreateContentDataStoreModel<ContentDataStoreModel>(content, queryParameters);
return _objectSerializer.Serialize(creator);
}
}
I don't know what exactly did you need from your IContent content object but the code above will give you all the properties and some additional stuff.
Probably it would also work if you just used our IObjectSerializer, unless you have newtonsoft converters in your codebase.
Running on EpiSever.CMS 12.31.2
Recently we move the solution .net8 but facing issues when using the below code snippet:
---> System.NotSupportedException: Serialization and deserialization of 'System.Type' instances is not supported.