Convert Entry JSON to Entry Model

If you can access the JSON representation of an Entry, then you can convert that JSON to an Entry Model (https://deliverystack.wordpress.com/2020/07/20/net-core-headless-cms-entry-models-and-entry-model-classes/).

This functionality works well as a .NET extension method (https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods).

This example uses the Contentstack .NET SDK (https://www.contentstack.com/docs/platforms/dot-net/api-reference/api/index.html) for the following reasons:

The .NET SDKs for all headless CMS products provide similar functionality.

https://pastebin.com/zxJHscxF

This code uses the SerializerSettings property exposed by ContentstackClient. In this case, there may be nothing relevant in those settings. In other cases, this is a convenient way to avoid initializing a new SerializerSetttings, specifically to add JsonConverters for Modular Blocks and other features, the logic for which I would want to implement only once and in one place. To avoid exposing vendor-specific APIs in your Entry Models, you may want to copy the JObject to a property in your Entry Model after instantiating that Entry Model. Update: https://deliverystack.net/2020/07/23/expose-entry-json-from-entry-model/

This code also demonstrates how you can retrieve a single simple Field Value from the default Entry Model class. Because every Entry in Contentstack has a Title field, the default Entry Model class has a Title property that you can access. For other properties, you can access values from JSON through the Object property of the default Entry Model class or you may prefer to use the JObject exposed by the ToJson() method of that class, which returns the JObject used to deserialize the JSON to an Entry Model in this example. Optimally, you would use an Entry Model populated implicitly by deserialization rather than accessing the JSON directly in any manner.

https://pastebin.com/eC4San2X

Published by John West

I like Twizzlers and um

One thought on “Convert Entry JSON to Entry Model

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: