json_api_document 1.1.0

  • Readme
  • Changelog
  • Installing
  • 8

The project has moved to json_api (GitHub)!




JSON:API Document #

JSON:API is a specification for building APIs in JSON. This library implements the Document model and a Parser.

Document model #

The Document is a set of Dart classes describing the main JSON:API objects: Resources and Resource Identifiers, Primary Data, Relationships, etc. Use it to produce a valid JSON:API Document.

Put the following code to hello_world.dart:

import 'dart:convert';

import 'package:json_api_document/json_api_document.dart';

main() {
  final message =
      ResourceObject('messages', '1', attributes: {'text': 'Hello world'});
  final primaryData =
      ResourceData(message, self: Link(Uri.parse('/messages/1')));

  final doc = Document(primaryData);

  print(json.encode(doc));
}

Now execute:

dart hello_world.dart

It will produce the following output (formatted for clarity):

{
  "data": {
    "type":"messages",
    "id":"1",
    "attributes": {
      "text":"Hello world"
    }
  },
  "links": {
    "self":"/messages/1"
  }
}

Parser #

The Parser converts parsed JSON objects to Documents.

Put this code to hello_world_parse.dart:

import 'dart:convert';
import 'dart:io';

import 'package:json_api_document/parser.dart';

void main() async {
  // Read the json from the standard input
  final jsonString = await stdin.transform(Utf8Decoder()).join();

  // Convert the json to a Dart object
  final jsonObject = json.decode(jsonString);

  // Parse the object into a document
  final doc = JsonApiParser().parseResourceDocument(jsonObject);

  // Print the attributes
  doc.data.resourceObject.attributes.forEach((k, v) => print('$k: $v'));
}

Now let's run them together feeding the output of the first program to the second:

dart hello_world.dart | dart hello_world_parse.dart

This will output the parsed attributes:

text: Hello world

Please refer to the example and test folders for usage examples.

Changelog #

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog and this project adheres to Semantic Versioning.

1.1.0 - 2019-08-12

Added #

  • Resource.withId()

1.0.2 - 2019-04-13

Fixed #

  • ToMany.toIdentifiers() should return List

1.0.1 - 2019-04-03

Fixed #

  • Do not send id:null in ResourceObject (#58)

1.0.0 - 2019-03-29

Changed #

  • Nothing. Just a formal v1 release

0.6.0 - 2019-03-25

Changed #

  • Renamed the main library file: document.dart -> json_api_document.dart

0.5.0 - 2019-03-24

Changed #

  • Total BC-breaking rewrite of the library. It is split into 3 parts: the document, the parser and the validator.

0.3.10 - 2019-01-20

Added #

  • Exposed Relationship class

0.3.9 - 2019-01-08

Fixed #

  • Bug when IdentifierList data could not be parsed due to insufficient type information in runtime

0.3.8 - 2019-01-07

Added #

  • preferResource flag in DataDocument.fromJson()
  • Friendlier toString()

0.3.5 - 2019-01-05

Added #

  • PrimaryData and its subclasses are now exposed

0.3.4 - 2019-01-05

Fixed #

  • Document.fromJson does not recognize null resource id

0.3.3 - 2019-01-05

Added #

  • Document.mediaType constant

0.3.2 - 2018-11-05

Changed #

  • Minor documentation improvements

0.3.0 - 2018-11-05

Added #

  • Parsing capabilities.
  • A few accessors to Meta, Attributes, Relationships

Removed #

  • Link.isObject
  • Link.meta

0.2.2 - 2018-10-09

Added #

  • Enforce naming rules on relationships
  • Included resources are checked for duplicates
  • LinkObject
  • Pagination links for data collection documents
  • Resource fields uniqueness enforcement

0.2.1 - 2018-10-05

Added #

  • Compound documents
  • Meta property to Identifier
  • Prohibit creating empty Api objects

0.2.0 - 2018-10-03 #

Added #

  • Initial usable implementation

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  json_api_document: ^1.1.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

Alternatively, your editor might support pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:json_api_document/json_api_document.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
15
Health:
Code health derived from static analysis. [more]
--
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
--
Overall:
Weighted score of the above. [more]
8
Learn more about scoring.

This package is not analyzed, because it is discontinued.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0