Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at

  • David Dimmer 76 posts 134 karma points
    Jul 01, 2016 @ 23:39
    David Dimmer

    PublishedContentModel be Serialized?

    Can “PublishedContentModel” classes emitted from the ModelBuilder be serialized and used as return types from custom WebAPI controllers derived from UmbracoApiController?

    • If yes, are there JsonConvert overrides needed? Which ones?

    • Must you hand-craft POCOS to use Ditto when mapping from the type returned from Umbraco.TypedContent (IPublishedContent) in order to serialize without stack overflow errors?

    Thanks, David Dimmer

  • Alex Skrypnyk 6048 posts 23119 karma points MVP 5x admin c-trib
    Jul 04, 2016 @ 20:04
    Alex Skrypnyk

    Hi David,

    Have the same problem, did you solve the issue?

    Don't want to write AutoMapper mappings for each class, return modelBuilder classes from the API would be perfect.


  • Chris 47 posts 127 karma points
    Jul 31, 2016 @ 23:01

    We're in the same boat. When attempting to serialize any PublishedContentModel a stackoverflowexception occurs at runtime. Anyone have some feedback on this? I'm wondering if its a bug or simply due to the PublishedContentModels inheritance structure?

  • Michaela Ivanova 12 posts 104 karma points
    Aug 02, 2016 @ 08:53
    Michaela Ivanova

    Here is how you can serialize a collection of IPublishedContent:

    var collection = Model.Content.Children();
    var itemsToSerialize = new List<dynamic>();
    foreach (var s in collection)
        dynamic exObj = new System.Dynamic.ExpandoObject();
        var dictionary = exObj as IDictionary<string, object>;
        s.Properties.ForEach(p => dictionary[p.PropertyTypeAlias] = p.Value);
        var sJson = Newtonsoft.Json.JsonConvert.SerializeObject(exObj);
    var jsonCollection = Newtonsoft.Json.JsonConvert.SerializeObject(itemsToSerialize);
  • Chris 47 posts 127 karma points
    Aug 03, 2016 @ 15:42

    Thanks, Michaela, we'll give this a try! Any thoughts as to why the default PublishedContentModel type in U7.4 can't be serialized directly?


  • Casper 69 posts 306 karma points
    Aug 14, 2016 @ 19:48
Please Sign in or register to post replies

Write your reply to: