All Projects → eredo → Dartson

eredo / Dartson

Licence: mit
Dartson is a Dart library that can be used to convert Dart objects into a JSON string.

Programming Languages

dart
5743 projects

Projects that are alternatives of or similar to Dartson

Airframe
Essential Building Blocks for Scala
Stars: ✭ 442 (+466.67%)
Mutual labels:  json, serialization
Jackson Module Kotlin
Module that adds support for serialization/deserialization of Kotlin (http://kotlinlang.org) classes and data classes.
Stars: ✭ 830 (+964.1%)
Mutual labels:  json, serialization
Iguana
universal serialization engine
Stars: ✭ 481 (+516.67%)
Mutual labels:  json, serialization
Handyjson
A handy swift json-object serialization/deserialization library
Stars: ✭ 3,913 (+4916.67%)
Mutual labels:  json, serialization
Fhir.js
Node.JS library for serializing/deserializing FHIR resources between JS/JSON and XML using various node.js XML libraries
Stars: ✭ 61 (-21.79%)
Mutual labels:  json, serialization
Jsoniter Scala
Scala macros for compile-time generation of safe and ultra-fast JSON codecs
Stars: ✭ 410 (+425.64%)
Mutual labels:  json, serialization
Fastjson
A fast JSON parser/generator for Java.
Stars: ✭ 23,997 (+30665.38%)
Mutual labels:  json, serialization
Bebop
An extremely simple, fast, efficient, cross-platform serialization format
Stars: ✭ 305 (+291.03%)
Mutual labels:  json, serialization
Tech1 Benchmarks
Java JMH Benchmarks repository. No Longer Supported.
Stars: ✭ 50 (-35.9%)
Mutual labels:  json, serialization
Eminim
JSON serialization framework for Nim, works from a Stream directly to any type and back. Depends only on stdlib.
Stars: ✭ 32 (-58.97%)
Mutual labels:  json, serialization
Leopotamgrouplibraryunity
Tools library for unity 3d game engine: animator graph helpers, serialization (json), localization, event routing (eventbus, ui actions), embedded scripting, uGui xml markup, threading, tweening, in-memory protection and other helpers (pure C#)
Stars: ✭ 373 (+378.21%)
Mutual labels:  json, serialization
Dictfier
Python library to convert/serialize class instances(Objects) both flat and nested into a dictionary data structure. It's very useful in converting Python Objects into JSON format
Stars: ✭ 67 (-14.1%)
Mutual labels:  json, serialization
Velocypack
A fast and compact format for serialization and storage
Stars: ✭ 347 (+344.87%)
Mutual labels:  json, serialization
Flatcc
FlatBuffers Compiler and Library in C for C
Stars: ✭ 434 (+456.41%)
Mutual labels:  json, serialization
Kim
Kim: A JSON Serialization and Marshaling framework
Stars: ✭ 326 (+317.95%)
Mutual labels:  json, serialization
Groot
From JSON to Core Data and back.
Stars: ✭ 533 (+583.33%)
Mutual labels:  json, serialization
Kotlinx.serialization
Kotlin multiplatform / multi-format serialization
Stars: ✭ 3,550 (+4451.28%)
Mutual labels:  json, serialization
Fspickler
A fast multi-format message serializer for .NET
Stars: ✭ 299 (+283.33%)
Mutual labels:  json, serialization
Play Json Extra
playframework2 json extra module. provide convenience functions for define Format, Reads, Writes
Stars: ✭ 20 (-74.36%)
Mutual labels:  json, serialization
Simplify Core
Simplify 为简化重复的JAVA代码而生,基于JDK8,无其它jar包依赖,提供序列化,json parse/generator,日期处理,asm && jdkproxy 实现动态代理功能 等常见操作。
Stars: ✭ 65 (-16.67%)
Mutual labels:  json, serialization

dartson

Pub Version Build Status Coverage Status

Dartson 1.0.0 is currently in alpha. The public API might be subject to change. For further details of potential breaks and a roadmap take a look at project 1.0.0.

Dartson is a dart library which converts Dart Objects into their JSON representation. It helps you keep your code clean of fromJSON and toJSON functions by providing a builder which generates the serialization methods.

Usage

Add the following lines to your pubspec.yaml in order to use dartson:

dependencies:
  dartson: ^1.0.0-alpha+4
  
dev_dependencies:
  build_runner: ^0.10.0

Dartson is using a central serializer instead of serializers for each object, therefore create a central file which refers the objects that need to be serialized:

import 'package:dartson/dartson.dart';
import 'package:some_dependency/some_class.dart';

import 'my_class.dart';

@Serializer(
  entities: [
    MyClass,
    SomeClass,
  ],
)
final Dartson<Map<String, dynamic>> serializer = _serializer$dartson;

Dartson encodes and decodes into a serializable Map (Map<String, dynamic>) by default. In order to encode and decode into a json string (in previous versions done by using Dartson.JSON) directly, call the useCodec method on the generated Dartson instance, which creates a new instance using the provided codec.

import 'dart:convert';

import 'package:dartson/dartson.dart';
import 'package:some_dependency/some_class.dart';

import 'my_class.dart';

@Serializer(
  entities: [
    MyClass,
    SomeClass,
  ],
)
final Dartson<String> serializer = _serializer$dartson.useCodec(json);

Private properties

It's not possible to encode / decode private properties. To set private properties, expose these within the constructor and provide a getter for encoding the entity.

Encoding / decoding lists

As of dartson >1.0.0 there are specific encodeList and decodeList methods. Because of type restrictions encodeList returns an Object and decodeList expects an Object. This should not cause any further actions when using json codec, however when working with the default serializer without any Codec, than a cast to List<Map<String, dynamic>> might be necessary when using the encodeList result.

main() {
  final result = serializer.encodeList([
	MyClass()..name = 'test1',
	MyClass()..name = 'test2',
  ]) as List<Map<String, dynamic>>;

  expect(result, allOf(isList, hasLength(2)));
  expect(result[0]['name'], 'test1');
  expect(result[1]['name'], 'test2');
}

Replacing entities

Sometimes entities are automatically generated and as such cannot contain any handwritten code, which could provide further logic and reduce complexity. This is where the replacement feature of dartson can help.

Here an example of an entity called Money which is replaced using MoneyImpl for replacing the operators.

import 'package:dartson/dartson.dart';
import 'package:dartson/transformers/date_time.dart';

// Imagine Money and Product couldn't be touched.
class Money {
  double net;
  double gross;
}

class Product {
  Money price;
  String name;
}


class MoneyImpl extends Money {
  operator +(dynamic ob) {
    if (obj is! Money) {
      throw TypeError();
    }
    
    net += ob.net;
    gross += ob.gross;
  }
}


@Serializer(
  entities: [
    Money,
    Product,
  ],
  replacements: {
    Money: MoneyImpl,
  },
  transformers: [
    DateTimeParser,
  ],
)
final Dartson serializer = _serializer$dartson;

Extending the serializer

Dartson supports extending serializers to provide a module approach. This is necessary to support functionality like deferred loading. This also may improve build times, so when changing an entity only a part of the serializer is regenerated.

serializer_init.dart

import 'dart:convert';

import 'package:dartson/dartson.dart';
import 'package:some_dependency/some_class.dart';

import 'my_class.dart';

@Serializer(
  entities: [
    MyClass,
    SomeClass,
  ],
)
final Dartson<String> serializer = _serializer$dartson.useCodec(json);

serializer_second.dart

import 'package:dartson/dartson.dart';

import 'other_class.dart';
import 'serializer_init.dart' as fs;

@Serializer(
  entities: [
    OtherClass,
  ],
)
final Dartson<String> serializer = fs.serializer.extend(_serializer$dartson);

Notice that extend provides a completely new instance of Dartson. Also the entities provided by the serializer on which extend was called can be overwritten by the entities used in the serializer passed as the argument (in this case: _serializer$dartson entities may overwrite fs.serializer entities).

Writting custom TypeTransformers

Transformers are used to encode / decode none serializable types that shouldn't be treated as objects / lists (for example DateTime).

/// A simple DateTime transformer which uses the toString() method.
class DateTimeParser implements TypeTransformer<String, DateTime> {
  // Make sure to add a constant constructor, because dartson will initiate all tranformers
  // as constant to improve dart2js compilation.
  const DateTimeParser();
  DateTime decode(String value) => DateTime.parse(value);
  String encode(DateTime value) => value.toString();
}

In order to use the TypeTransformer you need to register the transformer for the serializer:

import 'package:dartson/dartson.dart';
import 'package:dartson/transformers/date_time.dart';

import 'my_class.dart';

@Serializer(
  entities: [
    MyClass,
  ],
  transformers: [
    DateTimeParser,
  ],
)
final Dartson serializer = _serializer$dartson;

Roadmap for 1.0.0 alpha/beta

  • First alpha release evaluates and tests the reuse of json_serializable (refactorings during the alpha/beta will be necessary)
  • Additional functionality from proposals will be ported
  • Looking for feedback in regards of usability from users
  • Further benchmarking of potential bottlenecks because of single point of
    the builder

Further features planned

  • See doc/proposal for general features
  • Add tool to generate serializer.dart based on serializer.decode<T>() and serializer.encode(T) usage
  • Add analyzer plugin to detect potential issues of used entities which are not present in the serializer definition
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].