https://github.com/webbeta/nserializer
.NET Core port of our framework agnostic YAML based Serializer for Java (https://github.com/webbeta/Serializer)
https://github.com/webbeta/nserializer
csharp net-core serializer yml
Last synced: 2 months ago
JSON representation
.NET Core port of our framework agnostic YAML based Serializer for Java (https://github.com/webbeta/Serializer)
- Host: GitHub
- URL: https://github.com/webbeta/nserializer
- Owner: webbeta
- License: mit
- Created: 2020-04-01T20:57:20.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2022-12-08T10:04:55.000Z (over 3 years ago)
- Last Synced: 2025-03-02T09:07:05.647Z (over 1 year ago)
- Topics: csharp, net-core, serializer, yml
- Language: C#
- Homepage:
- Size: 57.6 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# webBeta NSerializer
[](https://travis-ci.org/webbeta/NSerializer)
[](https://www.codacy.com/gh/webbeta/NSerializer?utm_source=github.com&utm_medium=referral&utm_content=webbeta/NSerializer&utm_campaign=Badge_Grade)
[](LICENSE)
[](http://www.nuget.org/packages/webBeta.NSerializer)
* [What is webBeta NSerializer?](#what-is-webbeta-nserializer?)
* [Quick examples](#quick-examples)
* [License](#license)
## 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](https://github.com/webbeta/Serializer).
Steps to create a NSerializer instance.
1. Create a cache provider.
```csharp
public class MyCache : ICache
{
public string Get(string key)
{
return null;
}
public void Set(string key, string content)
{
}
public void Remove(string key)
{
}
}
```
2. Create a configuration provider.
```csharp
public class MyConfigurationProvider : IConfigurationProvider
{
private readonly Dictionary _conf;
public MyConfigurationProvider(Dictionary 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();
}
}
```
3. Create an environment provider.
```csharp
public class MyEnvironment : IEnvironment
{
public bool IsProd()
{
return true;
}
}
```
4. Then build the NSerializer.
```csharp
NSerializer serializer = NSerializerBuilder.Build()
.WithCache(cache)
.WithConfigurationProvider(configurationProvider)
.WithEnvironment(environment)
.Get();
```
5. Optionally, add a logger instance:
```csharp
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:
```csharp
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```):
```yaml
Foo.Bar.Foo:
virtual_properties:
GetCalc:
serialized_name: calc
groups: [barGroup]
properties:
bar:
groups: [fooGroup]
AutoPropertyString:
groups: [autoGroup]
```
Usage:
```csharp
serializer.Serialize(fooInstance, "fooGroup", "barGroup", "autoGroup");
// it will return
// {"bar": "foobar", "calc": 4, "autoPropertyString": "Foo Autoproperty!"}
```
## License
[MIT](LICENSE)