gson 0.1.2

  • Readme
  • Changelog
  • Example
  • Installing
  • 86

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]
82
Health:
Code health derived from static analysis. [more]
84
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
86
Learn more about scoring.

We analyzed this package on Jan 14, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.0
  • pana: 0.13.4

Health suggestions

Fix lib/decoder.dart. (-9.08 points)

Analysis of lib/decoder.dart reported 19 hints, including:

line 8 col 35: Unnecessary new keyword.

line 9 col 28: Unnecessary new keyword.

line 10 col 32: Unnecessary new keyword.

line 20 col 15: Unnecessary new keyword.

line 27 col 16: Unnecessary new keyword.

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

Analysis of lib/values.dart reported 6 hints, including:

line 18 col 25: Unnecessary new keyword.

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.

Fix lib/parsable.dart. (-1.99 points)

Analysis of lib/parsable.dart reported 4 hints:

line 133 col 11: Unnecessary new keyword.

line 136 col 25: Unnecessary new keyword.

line 153 col 9: Unnecessary new keyword.

line 156 col 23: Unnecessary new keyword.

Fix lib/gson.dart. (-1.49 points)

Analysis of lib/gson.dart reported 3 hints:

line 13 col 25: Unnecessary new keyword.

line 16 col 25: Unnecessary new keyword.

line 20 col 20: Unnecessary new keyword.

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