gson 0.1.2

  • Readme
  • Changelog
  • Example
  • Installing
  • 85

dart-gson #

Parses and encodes the json generated by GSON (Can parse minecraft's json)

This is just a simple parser and decoder.

Website (github pages): https://miniminelp.github.io/dart-gson/

Repository (github): https://github.com/MinimineLP/dart-gson

View dart docs: https://pub.dev/documentation/gson/latest/

View on pub: https://pub.dev/packages/gson

Installation #

Add this to your pubspec.yaml file

dependencies:
  gson: ^0.1.2

And to import use

import 'package:gson/gson.dart';

Usage #

To decode you can use

gson.decode("{...}");

and to decode you can use

gson.encode({...});

As dart has no different variable types for numbers (there are just num, int and double), the api gives you types. So if you want a double for example in the output you have to insert

gson.encode(new Double(1.0)); // >> 1.0d

Also the compiler gives these classes back to you, so you have to get the value property

gson.decode("1.0d").value; // >> 1.0

because booleans are displayed as bytes in gson, the boolean value is in the Byte type.

gson.encode(true) // >> 1b
gson.decode("1b").value // >> 1
gson.decode("1b").boolValue // >> true (and 0b will be false)

The program can't find the difference between the number 1 and true / the number 0 / false, because in the code it is the same.

For more examples see example/example.dart

License #

BSD 2-Clause License (See LICENSE)

Issues #

Please post issues here

Changelog #

0.1.2 #

  • Added beauty gson generation (customizable)
  • Improved examples
  • Fixes in Readme and codestyle

0.1.1 #

  • Fixes in Readme and codestyle (no new features)

0.1.0 #

  • Some Fixes in the decodeString and in the decodeNumber function that caused crashes
  • stopped the decoder from decoding numbers as strings
  • added documentation
  • improved code-style

0.0.1 #

  • Added encoder, Decoder

example/example.dart

import 'package:gson/gson.dart';

main(List<String> args) {
  // Basic encode example
  print(gson.encode({
    "a": "a",
    "b": ["hello", "world"],
    "c": 1,
    "d": false,
    "e": Byte(26)
  })); // >> {a:"a",b:["hello","world"],c:1,d:0b,e:26b}

  // As dart has no different variable types for numbers (there are just `num`, `int` and `double`), the api gives you types.
  // So if you want a double for example in the output you have to insert

  print(gson.encode(new Double(1.0))); // >> 1.0d

  // Also the compiler gives these classes back to you, so you have to get the value property
  print(gson.decode("1.0d").value); // >> 1.0d

  // because booleans are displayed as bytes in gson, the boolean value is in the Byte type.
  print(gson.encode(true)); // >> 1b
  print(gson.decode("1b").value); // >> 1
  print(gson.decode("1b").boolValue); // >> true (and 0b will be false)

  // For beautier gson use
  print(gson.encode({
    "a": "a",
    "b": ["hello", "world"],
    "c": 1,
    "d": false,
    "e": Byte(26)
  }, beautify: true));
  // >> {
  // >>   a: "a",
  // >>   b: [
  // >>     "hello",
  // >>     "world"
  // >>   ],
  // >>   c: 1,
  // >>   d: 0b,
  // >>   e: 26b
  // >> }

  // using indent you can controll the amount of spaces to insert

  print(gson.encode({
    "a": "a",
    "b": ["hello", "world"],
    "c": 1,
    "d": false,
    "e": Byte(26)
  }, beautify: true, indent: 4));
  // >> {
  // >>     a: "a",
  // >>     b: [
  // >>       "hello",
  // >>       "world"
  // >>     ],
  // >>     c: 1,
  // >>     d: 0b,
  // >>     e: 26b
  // >> }
}

Use this package as a library

1. Depend on it

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


dependencies:
  gson: ^0.1.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:gson/gson.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
72
Health:
Code health derived from static analysis. [more]
97
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
85
Learn more about scoring.

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

  • Dart: 2.5.1
  • pana: 0.12.21

Platforms

Detected platforms: Flutter, other

Primary library: package:gson/gson.dart with components: io.

Health suggestions

Fix lib/values.dart. (-2.48 points)

Analysis of lib/values.dart reported 5 hints:

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

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

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

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

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

Fix lib/encoder.dart. (-1 points)

Analysis of lib/encoder.dart reported 2 hints:

line 51 col 11: Use isEmpty instead of length

line 65 col 11: Use isEmpty instead of length

Dependencies

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