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
    0

    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

    Fyin.com

  • Alex Skrypnyk 6162 posts 24132 karma points MVP 8x admin c-trib
    Jul 04, 2016 @ 20:04
    Alex Skrypnyk
    0

    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.

    Thanks

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

    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
    1

    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);
        items.Add(sJson);
    }
    
    var jsonCollection = Newtonsoft.Json.JsonConvert.SerializeObject(itemsToSerialize);
    
  • Chris 47 posts 127 karma points
    Aug 03, 2016 @ 15:42
    Chris
    0

    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?

    Cheers,

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

Write your reply to:

Draft