Serializing an PublishedContentModel(modelsbuilder model) in V8
Hello there :)
Hoping to make a thread that a lot of people will get something out of, the overall goal of the thread is to be able to take a modelsbuilder model(underlying being a PublishedContentModel) and get it out as JSON. This was possible in V7 with some tinkering, but lets try and see how far we can get shall we?
So I have an object which comes from a modelsbuilder Model, so it inherits from PublishedContentModel, lets call it obj. Normally we could just put a model in to JSON.net and say give me JSON like:
@JsonConvert.SerializeObject(obj)
But both in V7 and V8 this gives you something like :
Self referencing loop detected for property....
But in V7 we could build a JSON.NET ContractResolver, where we could filter out the params it shouldn't serialize, and in this way make it into a serilizable model.
This class could look like this:
public class PublishedContentContractResolver : DefaultContractResolver
{
public new static readonly PublishedContentContractResolver Instance = new PublishedContentContractResolver();
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
JsonProperty property = base.CreateProperty(member, memberSerialization);
property.ShouldSerialize = instance =>
{
if (property.PropertyName == "CompositionAliases") return false;
if (property.PropertyName == "ContentSet") return false;
if (property.PropertyName == "PropertyTypes") return false;
if (property.PropertyName == "ObjectContent") return false;
if (property.PropertyName == "Properties") return false;
if (property.PropertyName == "Parent") return false;
if (property.PropertyName == "Children") return false;
if (property.PropertyName == "DocumentTypeId") return false;
if (property.PropertyName == "WriterName") return false;
if (property.PropertyName == "CreatorName") return false;
if (property.PropertyName == "WriterId") return false;
if (property.PropertyName == "CreatorId") return false;
if (property.PropertyName == "CreateDate") return false;
if (property.PropertyName == "UpdateDate") return false;
if (property.PropertyName == "Version") return false;
if (property.PropertyName == "SortOrder") return false;
if (property.PropertyName == "TemplateId") return false;
if (property.PropertyName == "IsDraft") return false;
if (property.PropertyName == "ItemType") return false;
if (property.PropertyName == "ContentType") return false;
if (property.PropertyName == "Url") return false;
if (property.PropertyName == "ContentSet") return false;
//making people on the other end of my api happy
property.PropertyName = StringUtils.ToCamelCase(property.PropertyName);
return true;
};
return property;
}
protected override JsonContract CreateContract(Type objectType)
{
JsonContract contract = base.CreateContract(objectType);
return contract;
}
}
Now we could go do something like this to use our brand new ContractResolver:
@{
PublishedContentContractResolver contractResolver = new PublishedContentContractResolver();
var settings = new JsonSerializerSettings();
settings.ContractResolver = contractResolver;
}
<pre>
@JsonConvert.SerializeObject(l, Formatting.Indented, settings)
</pre>
This would work in V7, job done, but in V8 something new is brewing, we get another YSOD, this time it saying the following:
So any people who have got a work around, an idea why or just a pointer? it says something about NuCache so don't know if Stephan has an idea or any other?
I may be misunderstanding the question but isn't there a new ChildrenForAllCultures property on IPublishedContent? and are you excluding that from serialization?
Just to close this, the problem wasn't with the serialization at all, but an umbraco error due to some types defined incorrectly ('element type' doc types had a non element type parent), which prevented umbraco from creating any models. The presence (if all's well, it shouldn't be there) and content of the file models.err in the App_Data\Models folder was the clue...
Serializing an PublishedContentModel(modelsbuilder model) in V8
Hello there :)
Hoping to make a thread that a lot of people will get something out of, the overall goal of the thread is to be able to take a modelsbuilder model(underlying being a PublishedContentModel) and get it out as JSON. This was possible in V7 with some tinkering, but lets try and see how far we can get shall we?
So I have an object which comes from a modelsbuilder Model, so it inherits from PublishedContentModel, lets call it obj. Normally we could just put a model in to JSON.net and say give me JSON like:
But both in V7 and V8 this gives you something like :
But in V7 we could build a JSON.NET ContractResolver, where we could filter out the params it shouldn't serialize, and in this way make it into a serilizable model.
This class could look like this:
Now we could go do something like this to use our brand new ContractResolver:
This would work in V7, job done, but in V8 something new is brewing, we get another YSOD, this time it saying the following:
So any people who have got a work around, an idea why or just a pointer? it says something about NuCache so don't know if Stephan has an idea or any other?
I may be misunderstanding the question but isn't there a new ChildrenForAllCultures property on IPublishedContent? and are you excluding that from serialization?
https://github.com/umbraco/Umbraco-CMS/blob/3bfd9b71e290354744d677440983ecd06c5c0788/src/Umbraco.Core/Models/PublishedContent/IPublishedContent.cs#L164
Hey Marc!
Thx for helping, ive just added it to the exclution and now it works!
THANK YOU :)
So for everyone needing a JSON serilization solution use thg following ContractResolver and go through the steps in my first post!
{ public new static readonly PublishedContentContractResolver Instance = new PublishedContentContractResolver(); protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization) { JsonProperty property = base.CreateProperty(member, memberSerialization);
Hi, I tried this first on Umbraco 7, and it worked perfectly (with the additional handling of the rich text editor, as described here https://our.umbraco.com/forum/using-umbraco-and-getting-started/100395-how-do-i-convert-rich-text-editor-doctype-properties-to-json-in-umbraco-8), but on Umbraco 8.2.2 - no properties are being serialized, I only get the key, Name, Id, level, path etc. properties. Did anyone else encounter this?
Just to close this, the problem wasn't with the serialization at all, but an umbraco error due to some types defined incorrectly ('element type' doc types had a non element type parent), which prevented umbraco from creating any models. The presence (if all's well, it shouldn't be there) and content of the file models.err in the App_Data\Models folder was the clue...
is working on a reply...