json_api_document 1.0.2

  • README.md
  • CHANGELOG.md
  • Installing
  • Versions
  • 79

Other JSON:API packages: Client | Server


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.

Unreleased #

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.0.2

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter 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';
  
Version Uploaded Documentation Archive
1.0.2 Apr 13, 2019 Go to the documentation of json_api_document 1.0.2 Download json_api_document 1.0.2 archive
1.0.1 Apr 3, 2019 Go to the documentation of json_api_document 1.0.1 Download json_api_document 1.0.1 archive
1.0.0+2 Apr 1, 2019 Go to the documentation of json_api_document 1.0.0+2 Download json_api_document 1.0.0+2 archive
1.0.0+1 Mar 30, 2019 Go to the documentation of json_api_document 1.0.0+1 Download json_api_document 1.0.0+1 archive
1.0.0 Mar 29, 2019 Go to the documentation of json_api_document 1.0.0 Download json_api_document 1.0.0 archive
0.6.0+1 Mar 28, 2019 Go to the documentation of json_api_document 0.6.0+1 Download json_api_document 0.6.0+1 archive
0.6.0 Mar 26, 2019 Go to the documentation of json_api_document 0.6.0 Download json_api_document 0.6.0 archive
0.5.0 Mar 25, 2019 Go to the documentation of json_api_document 0.5.0 Download json_api_document 0.5.0 archive
0.4.0 Mar 25, 2019 Go to the documentation of json_api_document 0.4.0 Download json_api_document 0.4.0 archive
0.3.10 Jan 21, 2019 Go to the documentation of json_api_document 0.3.10 Download json_api_document 0.3.10 archive

All 38 versions...

Popularity:
Describes how popular the package is relative to other packages. [more]
59
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
79
Learn more about scoring.

We analyzed this package on Jun 25, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.3.2
  • pana: 0.12.18

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:json_api_document/json_api_document.dart.

Health suggestions

Fix lib/src/document.dart. (-0.50 points)

Analysis of lib/src/document.dart reported 1 hint:

line 63 col 7: DO use curly braces for all flow control structures.

Maintenance suggestions

Maintain an example.

None of the files in the package's example/ directory matches known example patterns.

Common filename patterns include main.dart, example.dart, and json_api_document.dart. Packages with multiple examples should provide example/README.md.

For more information see the pub package layout conventions.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
Dev dependencies
json_matcher ^0.2.3
test ^1.6.1