PiranhaCMS / piranha.core

Piranha CMS is the friendly editor-focused CMS for .NET that can be used both as an integrated CMS or as a headless API.
http://piranhacms.org
MIT License
1.96k stars 551 forks source link

Any way of adding the standard blocks type into the json? #518

Closed stefanolsenn closed 5 years ago

stefanolsenn commented 5 years ago

Any way of easily adding the type/name/classname of the block when it gets serialized to json, without extending the JsonSerializer?

tidyui commented 5 years ago

Hi there! Could you clarify a bit. Are you talking about how fields are serialized to the database or how a model is serialized when you return it from an ApiController?

Regards

stefanolsenn commented 5 years ago

The last part, how to serialize the standard blocks Type/name when it gets returned from the ApiController. Right now a HtmlBlock holds a body attribute, what I want is a attribute besides that like "type": "HtmlBlock" or something like that.

tidyui commented 5 years ago

Good point! I'm guessing you're building the client application in javascript and since the block collection is totally dynamic you need to know how to render the data.

stefanolsenn commented 5 years ago

Exactly 👍

stefanolsenn commented 5 years ago

@tidyui Do you have any temporary workaround for this in mind?

tidyui commented 5 years ago

Not at the moment without adding custom serializers to JSON.Net. I'm adding this to the 6.0 release. We could release a first alpha containing this, however it will contain breaking changes as the repository layer has been totally restructured and is now async.

However if you're planning on updating to 6.0 when it's released it could always be good to try it out :)

tidyui commented 5 years ago

Just so I understand it right, the type should probably go on both block and field so you can distinguish both the field value and it's context.

tidyui commented 5 years ago

With the new functionality the Start Page model of the example site looks like this. Is this what you had in mind?

{
    "hero": {
        "subtitle": {
            "value": "By developers - for developers",
            "type": "Piranha.Extend.Fields.StringField"
        },
        "primaryImage": {
            "type": "Piranha.Extend.Fields.ImageField",
            "id": "339d06b3-f7ad-4a9c-876e-2d9a07fc601a",
            "hasValue": true
        },
        "ingress": {
            "value": "<p>A lightweight & unobtrusive CMS for ASP.NET Core.</p><p><small>Stable version 5.2.1 - 2018-10-17 - <a href=\"https://github.com/piranhacms/piranha.core/wiki/changelog\" target=\"_blank\">Changelog</a></small></p>",
            "type": "Piranha.Extend.Fields.HtmlField"
        }
    },
    "teasers": [
        {
            "title": {
                "value": "Cross Platform",
                "type": "Piranha.Extend.Fields.StringField"
            },
            "subTitle": null,
            "image": {
                "type": "Piranha.Extend.Fields.ImageField",
                "id": "d5ab5c7d-a1d2-41be-b232-642f3b9bc6d8",
                "hasValue": true
            },
            "body": {
                "value": "<p>Built for <code>NetStandard</code> and <code>AspNet Core</code>, Piranha CMS can be run on Windows, Linux and Mac OS X.</p>",
                "type": "Piranha.Extend.Fields.HtmlField"
            }
        },
        {
            "title": {
                "value": "Super Fast",
                "type": "Piranha.Extend.Fields.StringField"
            },
            "subTitle": null,
            "image": {
                "type": "Piranha.Extend.Fields.ImageField",
                "id": "ac729a16-5f9e-4e1b-b437-d070b98a6c98",
                "hasValue": true
            },
            "body": {
                "value": "<p>Designed from the ground up for super-fast performance using <code>EF Core</code> and optional Caching.</p>",
                "type": "Piranha.Extend.Fields.HtmlField"
            }
        },
        {
            "title": {
                "value": "Open Source",
                "type": "Piranha.Extend.Fields.StringField"
            },
            "subTitle": null,
            "image": {
                "type": "Piranha.Extend.Fields.ImageField",
                "id": "aa085e63-8382-43b6-9e03-27433b138b08",
                "hasValue": true
            },
            "body": {
                "value": "<p>Everything is Open Source and released under the <code>MIT</code> license for maximum flexibility.</p>",
                "type": "Piranha.Extend.Fields.HtmlField"
            }
        }
    ],
    "quotes": [],
    "latestPost": null,
    "isStartPage": true,
    "siteId": "34de8097-f3b8-43a1-8333-fa722af8a683",
    "contentType": "Page",
    "parentId": null,
    "sortOrder": 0,
    "navigationTitle": "Home",
    "isHidden": false,
    "redirectUrl": null,
    "redirectType": 0,
    "originalPageId": null,
    "blocks": [
        {
            "body": {
                "type": "Piranha.Extend.Fields.ImageField",
                "id": "c50b68f7-bf68-4e20-ae3f-fb87131dfad3",
                "hasValue": true
            },
            "type": "Piranha.Extend.Blocks.ImageBlock"
        },
        {
            "body": {
                "value": "<h2>What is Piranha CMS</h2>\n<p>Piranha CMS is a <strong>lightweight</strong>, <strong>cross-platform</strong> CMS <strong>library</strong> for <code>NetStandard 2.0</code>, <code>.NET Core</code> &amp; <code>Entity Framework Core</code>. It can be used to add CMS functionality to your existing application or to build a new website from scratch. It has an extensible &amp; pluggable architecture that can support a wide variety of runtime scenarios.</p>\n<p>Piranha CMS is totally <strong>package based</strong> and available on <code>NuGet</code>. You can read more about the different packages available in the documentation.</p>\n",
                "type": "Piranha.Extend.Fields.HtmlField"
            },
            "type": "Piranha.Extend.Blocks.HtmlBlock"
        },
        {
            "body": {
                "value": "<h2>Getting started</h2>\n<p>The easiest way to get started is to install the project templates from <code>NuGet</code>.</p>\n<pre><code>dotnet new -i Piranha.BasicWeb.CSharp\ndotnet new -i Piranha.Blog.CSharp\n</code></pre>\n<p>After that is done, just create a new project and get going.</p>\n<pre><code>dotnet new piranha\ndotnet restore\ndotnet run\n</code></pre>\n",
                "type": "Piranha.Extend.Fields.HtmlField"
            },
            "type": "Piranha.Extend.Blocks.HtmlBlock"
        }
    ],
    "slug": "home",
    "permalink": "/home",
    "metaKeywords": "Piranha, Piranha CMS, CMS, AspNetCore, DotNetCore, MVC, .NET, .NET Core",
    "metaDescription": "Piranha is the fun, fast and lightweight framework for developing cms-based web applications with AspNetCore.",
    "route": "/teaserpage",
    "published": "2019-03-05T13:59:27.271443",
    "id": "61456600-8560-4e21-a90a-e8dba70e8095",
    "typeId": "TeaserPage",
    "title": "Piranha CMS - Open Source, Cross Platform Asp.NET Core CMS",
    "created": "2019-03-05T13:59:27.386608",
    "lastModified": "2019-03-05T13:59:27.340651"
}
tidyui commented 5 years ago

Please note I updated the comment above as I realized fields within blocks were missing their type.

Regards

tidyui commented 5 years ago

After talking it through with my colleague @peppelorum who is also in the javascript-bending-business I realized that exporting type of field level was totally unnecessary. The serialized model now looks like this:

{
    "hero": {
        "subtitle": {
            "value": "By developers - for developers"
        },
        "primaryImage": {
            "id": "339d06b3-f7ad-4a9c-876e-2d9a07fc601a",
            "hasValue": true
        },
        "ingress": {
            "value": "<p>A lightweight & unobtrusive CMS for ASP.NET Core.</p><p><small>Stable version 5.2.1 - 2018-10-17 - <a href=\"https://github.com/piranhacms/piranha.core/wiki/changelog\" target=\"_blank\">Changelog</a></small></p>"
        }
    },
    "teasers": [
        {
            "title": {
                "value": "Cross Platform"
            },
            "subTitle": null,
            "image": {
                "id": "d5ab5c7d-a1d2-41be-b232-642f3b9bc6d8",
                "hasValue": true
            },
            "body": {
                "value": "<p>Built for <code>NetStandard</code> and <code>AspNet Core</code>, Piranha CMS can be run on Windows, Linux and Mac OS X.</p>"
            }
        },
        {
            "title": {
                "value": "Super Fast"
            },
            "subTitle": null,
            "image": {
                "id": "ac729a16-5f9e-4e1b-b437-d070b98a6c98",
                "hasValue": true
            },
            "body": {
                "value": "<p>Designed from the ground up for super-fast performance using <code>EF Core</code> and optional Caching.</p>"
            }
        },
        {
            "title": {
                "value": "Open Source"
            },
            "subTitle": null,
            "image": {
                "id": "aa085e63-8382-43b6-9e03-27433b138b08",
                "hasValue": true
            },
            "body": {
                "value": "<p>Everything is Open Source and released under the <code>MIT</code> license for maximum flexibility.</p>"
            }
        }
    ],
    "quotes": [],
    "latestPost": null,
    "isStartPage": true,
    "siteId": "34de8097-f3b8-43a1-8333-fa722af8a683",
    "contentType": "Page",
    "parentId": null,
    "sortOrder": 0,
    "navigationTitle": "Home",
    "isHidden": false,
    "redirectUrl": null,
    "redirectType": 0,
    "originalPageId": null,
    "blocks": [
        {
            "body": {
                "id": "c50b68f7-bf68-4e20-ae3f-fb87131dfad3",
                "hasValue": true
            },
            "type": "Piranha.Extend.Blocks.ImageBlock"
        },
        {
            "body": {
                "value": "<h2>What is Piranha CMS</h2>\n<p>Piranha CMS is a <strong>lightweight</strong>, <strong>cross-platform</strong> CMS <strong>library</strong> for <code>NetStandard 2.0</code>, <code>.NET Core</code> &amp; <code>Entity Framework Core</code>. It can be used to add CMS functionality to your existing application or to build a new website from scratch. It has an extensible &amp; pluggable architecture that can support a wide variety of runtime scenarios.</p>\n<p>Piranha CMS is totally <strong>package based</strong> and available on <code>NuGet</code>. You can read more about the different packages available in the documentation.</p>\n"
            },
            "type": "Piranha.Extend.Blocks.HtmlBlock"
        },
        {
            "body": {
                "value": "<h2>Getting started</h2>\n<p>The easiest way to get started is to install the project templates from <code>NuGet</code>.</p>\n<pre><code>dotnet new -i Piranha.BasicWeb.CSharp\ndotnet new -i Piranha.Blog.CSharp\n</code></pre>\n<p>After that is done, just create a new project and get going.</p>\n<pre><code>dotnet new piranha\ndotnet restore\ndotnet run\n</code></pre>\n"
            },
            "type": "Piranha.Extend.Blocks.HtmlBlock"
        }
    ],
    "slug": "home",
    "permalink": "/home",
    "metaKeywords": "Piranha, Piranha CMS, CMS, AspNetCore, DotNetCore, MVC, .NET, .NET Core",
    "metaDescription": "Piranha is the fun, fast and lightweight framework for developing cms-based web applications with AspNetCore.",
    "route": "/teaserpage",
    "published": "2019-03-05T13:59:27.271443",
    "id": "61456600-8560-4e21-a90a-e8dba70e8095",
    "typeId": "TeaserPage",
    "title": "Piranha CMS - Open Source, Cross Platform Asp.NET Core CMS",
    "created": "2019-03-05T13:59:27.386608",
    "lastModified": "2019-03-05T13:59:27.340651"
}
stefanolsenn commented 5 years ago

Hi @tidyui That is exactly what I had in mind, awesome! Do you have an ETA on version 6?