json_api_document 1.1.0 copy "json_api_document: ^1.1.0" to clipboard
json_api_document: ^1.1.0 copied to clipboard

discontinued

JSON:API Document Model and Parser for Flutter, Web and VM. Supports JSON:API v1.0 (http://jsonapi.org)

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.

0
likes
20
pub points
0%
popularity

Publisher

unverified uploader

JSON:API Document Model and Parser for Flutter, Web and VM. Supports JSON:API v1.0 (http://jsonapi.org)

Repository (GitHub)
View/report issues

License

MIT (license)

More

Packages that depend on json_api_document