Giter Site home page Giter Site logo

nserializer's Introduction

webBeta NSerializer

Build Status Codacy Badge GitHub license NuGet

What is webBeta NSerializer?

YML based serializer for .Net Core. Based on JMS serializer https://jmsyst.com/libs/serializer/master/reference/yml_reference

It is a fork of our YML based serializer for Java.

Steps to create a NSerializer instance.

  1. Create a cache provider.
public class MyCache : ICache
{
    public string Get(string key)
    {
        return null;
    }

    public void Set(string key, string content)
    {
    }

    public void Remove(string key)
    {
    }
}
  1. Create a configuration provider.
public class MyConfigurationProvider : IConfigurationProvider
{
    private readonly Dictionary<string, object> _conf;

    public MyConfigurationProvider(Dictionary<string, object> conf)
    {
        _conf = conf;
    }

    public bool GetBoolean(string key, bool defaultValue)
    {
        return !_conf.ContainsKey(key) ? defaultValue : bool.Parse(_conf[key].ToString());
    }

    public string GetString(string key, string defaultValue)
    {
        return !_conf.ContainsKey(key) ? defaultValue : _conf[key].ToString();
    }
}
  1. Create an environment provider.
public class MyEnvironment : IEnvironment
{    
    public bool IsProd()
    {
        return true;
    }
}
  1. Then build the NSerializer.
NSerializer serializer = NSerializerBuilder.Build()
        .WithCache(cache)
        .WithConfigurationProvider(configurationProvider)
        .WithEnvironment(environment)
        .Get();
  1. Optionally, add a logger instance:
public class MyLogger : ILogger
{    
    public Level GetLevel()
    {
        return Level.ERROR;
    }

    public void SetLevel(Level level) 
    {
    }

    public void Error(string message)
    {
    }
}

serializer.SetLogger(logger);

Quick examples

Class Foo:

namespace Foo.Bar
{
    public class Foo {
        
        private string bar = "foobar";

        public string AutoPropertyString { get; set; } = "Foo Autoproperty!";
        
        public string GetBar() {
            return bar;
        }
        
        public long GetCalc() {
            return 2 + 2;
        }
        
    }
}

Yml file (named Foo.Bar.Foo.yml):

Foo.Bar.Foo:
  virtual_properties:
    GetCalc:
      serialized_name: calc
      groups: [barGroup]
  properties:
    bar:
      groups: [fooGroup]
    AutoPropertyString:
      groups: [autoGroup]

Usage:

serializer.Serialize(fooInstance, "fooGroup", "barGroup", "autoGroup");

// it will return
// {"bar": "foobar", "calc": 4, "autoPropertyString": "Foo Autoproperty!"}

License

MIT

nserializer's People

Contributors

mowcixo avatar renovate[bot] avatar dependabot-preview[bot] avatar

Stargazers

 avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.