Copied to clipboard

Flag this post as spam?

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


  • Lee Kelleher 3884 posts 14615 karma points MVP 9x admin c-trib
    Jun 30, 2019 @ 16:01
    Lee Kelleher
    0

    How to deserialize JSON with LightInject dependency injection?

    Hey there Umbraco 8 fans!

    I'm struggling to figure out how to do dependency injection (LightInject) when deserializing JSON to a C# class object.

    My use-case is that I have a JSON string, which I need to deserialize to an object that I can make method calls - and use Umbraco service APIs.

    Here's what I have so far...

    JSON

    {
      "Foo": "just work"
    }
    

    C# Class

    public class MyClass
    {
      private readonly ILogger _logger;
    
      public MyClass(ILogger logger)
      {
        _logger = logger;
      }
    
      public string Foo { get; set; }
    
      public void DoSomething()
      {
        _logger.Debug<MyClass>($"Hello, I'd really like this to... {Foo}");
      }
    }
    

    At this point, I started reading up about JSON.NET ContractResolvers, and how to configure them for Dependency Injection...

    https://www.newtonsoft.com/json/help/html/DeserializeWithDependencyInjection.htm

    It should go a little something like this... But! this is where I got stuck... I have no idea how to do the custom ContractResolver part!

    var settings = new JsonSerializerSettings
    {
      ContractResolver = new MyContractResolver() // HEP ME!
    };
    
    var json = ... // from above
    var foo = JsonConvert.DeserializeObject<MyClass>(json, settings);
    
    foo.DoSomething();
    

    Can anybody shed any light* on this?

    ...or it could be that I'm taking the wrong approach in the first place. I'm open to alternative suggestions.

    Cheers,
    - Lee

  • Mario 111 posts 709 karma points c-trib
    Jul 01, 2019 @ 01:11
    Mario
    1

    Hi Lee,

    This is not going to reply to your question about LightInject but, can't you use the Current object to get the Umbraco services instead using DI?

    That's at least what the docs recommend: https://our.umbraco.com/documentation/Getting-Started/Code/Debugging/Logging/#writing-to-the-log

    EDIT

    Are you happy with something like this?

    //register your type
    public class LighInjectComposer : IUserComposer
    {
        public void Compose(Composition composition)
        {
            composition.Register(typeof(MyClass));
        }
    }
    

    Then use this :

    MyClass instance = (MyClass)Current.Factory.GetInstance(typeof(MyClass));
    JsonConvert.PopulateObject(json, instance);
    
  • Lee Kelleher 3884 posts 14615 karma points MVP 9x admin c-trib
    Jul 01, 2019 @ 05:52
    Lee Kelleher
    0

    Cool, thanks for the suggestion Mario.

    I should have mentioned that I'm already using Current.Logger in my class method, oops.

    Thanks for the code snippet, appreciated. I wasn't aware of the PopulateObject method, nice! I'll give it a try.

    Cheers,
    - Lee

Please Sign in or register to post replies

Write your reply to:

Draft