GithubHelp home page GithubHelp logo

miniminelp / dart-gson Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 2.0 444 KB

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

License: BSD 2-Clause "Simplified" License

Dart 100.00%

dart-gson's Introduction

Forks Stargazers Issues BSD-2-Clause

dart-gson

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

This is just a simple parser and encoder.

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

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

View dart docs: https://miniminelp.github.io/dart-gson/doc/api/

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

Installation

Add this to your pubspec.yaml file

dependencies:
  gson: ^0.1.6

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

dart-gson's People

Contributors

focccus avatar miniminelp avatar nsc-de avatar schueler209 avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

ngima stevertus

dart-gson's Issues

support double data without "d" suffix

example:

import 'package:gson/gson.dart';

main()
{
var a = gson.decode('{aa:1.1}');
print(a);
}

error:
Unhandled exception:
type 'double' is not a subtype of type 'int'
#0 GsonDecoder.decodeNumber (package:gson/decoder.dart:230:31)
#1 GsonDecoder.decode (package:gson/decoder.dart:40:14)
#2 GsonDecoder.decodeMap (package:gson/decoder.dart:125:20)
#3 GsonDecoder.decode (package:gson/decoder.dart:25:14)
#4 Gson.decode (package:gson/gson.dart:80:20)
#5 main (file:///D:/Code/TestDart/bin/main.dart:5:16)
#6 _startIsolate. (dart:isolate-patch/isolate_patch.dart:301:19)
#7 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12)

Boolean value was endcode to byte value

I used your library to save the Object, it's okay.

Then I recognize there is different related to boolean value as below Log description :

setUser(User u) async {
    SharedPreferences p = await SharedPreferences.getInstance();

    print("setUser() : ${u.toJson()}");
    //  ${u.toJson()} : {isVIP: true, displayName: X}

    p.setString(USER, gsonEncode(u.toJson()));
    //  After gsonEncode : {isVIP: 1b, displayName: X}.  // I read it from Prefs.
  }

It looks like your library convert [true|false] value to [1b|0b] value.

Could you could keep its original value?
since It help me get it boolean value from Prefs. correct.

Map<String, dynamic> json = gsonDecode(result);

        User d = User.fromJson(json);

        return d;
class User {

   static const KEY_IS_VIP = 'isVIP';
  static const KEY_DISPLAY_NAME = 'displayName';

  final bool isVIP;

  final String displayName;

  User({this.isVIP, this.displayName});

  factory User.fromJson(Map<String, dynamic> json) {
    return User(
      isVIP : json['$KEY_IS_VIP'] as bool,
      displayName : json['$KEY_DISPLAY_NAME'],
    );
  }
}

p/s : Version 1.0.4

Can not update to latest version - version 0.1.5

Hello,

Did you update your library as your quote :

gson: ^0.1.5

Since I get error here :

Because flutter_nct depends on gson ^0.1.5 which doesn't match any versions, version solving failed.
pub get failed (1; Because flutter_nct depends on gson ^0.1.5 which doesn't match any versions, version solving failed.)

p/s : I upgrade from version 0.1.4.

Could not get terminal size

json str is "{url: , size: 0, second: 3, mediaId: null, downloadFlag: null}"
gson.decode(this json str), throw below exception:

unhandled exception:
StdoutException: Could not get terminal size, OS Error: Not a typewriter, errno = 25
#0 Stdout._terminalSize (dart:io-patch/stdio_patch.dart:147:7)
#1 Stdout._terminalColumns (dart:io-patch/stdio_patch.dart:140:35)
#2 Stdout.terminalColumns (dart:io/stdio.dart:232:30)
#3 terminalColumns (package:gson/terminal_vm.dart:4:35)

NullSafety

Is your feature request related to a problem? Please describe.
I cant be able to use dart-gson right now because it does not support null safety and i really need it

Describe the solution you'd like
Just support null safety

Describe alternatives you've considered
i don't have any alternative

Additional context
null safety null safety null safety please

boolean values do not move the position

Describe the bug
After parsing a boolean value, true or false, it does not move the position so it does not find the next comma and throws an error.

To Reproduce
Steps to reproduce the behavior:

  1. encode a JSON with a boolean value
  2. run 'gson.decode() in Dart.

Expected behavior
It should parse correctly and move the position.

Desktop (please complete the following information):
Android

Smartphone (please complete the following information):
Pixel 6 Pro

I tried to add 4 p.skip() for true and 5 p.skip() for false before returning the value locally and that fixed the issue.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.