Coder Social home page Coder Social logo

Comments (2)

lrhn avatar lrhn commented on July 28, 2024

A platform independent ability to parse 64-bit integers (just signed or unsigned too?) requires parsing into a type that is cross-platform.

We don't have an Int64 class in the platform libraries. There is a package for that, and a JSON parser using that package could probably do something reasonable.
(Although I'd prefer decoding to a different Int64 implementation which doesn't try to implement int operations. More like an Int64Value that can be converted to string, to int ... with all the inherent losses if on web, to Int64, or written into typed data, but doesn't have to be optimized for doing operations on it.)

So maybe that's the idea:

  • Introduce Int64Value in the platform libraries, with no guarantees about the implementation (but it'll be an extension type on int on native, and something else on web, like two 32-bit integers). Only supported operation is ==.
  • Introduce a JSON parser (perhaps a more efficient one than the one we have today) which can provide the values of int fields in a configurable way, with one option being Int64Value. (Generally have the parser provide interpretations of the source in whatever way the user wants, rather than build list/map structures of values.)
  • Allow converting Int64Value to any number of types, including BigInt and Int64 from the package (support added by that package).
  • Profit!

from sdk.

austinmilt avatar austinmilt commented on July 28, 2024

@lrhn that all sounds like a solid fix to me, and thanks for the thoughtful reply. The biggest bang for the buck IMO is

(Generally have the parser provide interpretations of the source in whatever way the user wants, rather than build list/map structures of values.)

The JSON spec already provides a relatively short list of possible element types, so being able to provide custom mapping from those types would be really powerful, e.g. (without considering efficiency)

abstract class JsonParser {
  static JsonParser defaultParser = BaseJsonParser();

  dynamic fromNumber(JsonNumber element);
  dynamic fromObject(JsonObject element);
  // ... etc for other JSON element types

  JsonElement toJson(dynamic value);

  dynamic fromJson(JsonElement element) {
    // builds an object from the mapping functions for each element type
  }

  JsonElement _parseJsonString(String value) {
    // parses the raw string into a JsonElement before
    throw UnimplementedError();
  }
}

class BaseJsonParser extends JsonParser {
  @override
  double fromNumber(JsonNumber element) {
    return double.parse(element.value);
  }

  @override
  Map<String, dynamic> fromObject(JsonObject element) {
    final Map<String, dynamic> result = {};
    for (MapEntry<JsonName, JsonElement> entry in element.value.entries) {
      // ... recursion
    }
    return result;
  }

  @override
  JsonElement toJson(dynamic value) {
    // switch case on type of value and return an appropriate conversion
    throw UnimplementedError();
  }
}

class JsonNumber implements JsonElement {
  JsonNumber(this.value);
  final String value;

  @override
  String encode() {
    return value.toString();
  }
}

class JsonObject implements JsonElement {
  JsonObject(this.value);
  final Map<JsonName, JsonElement> value;

  @override
  String encode() {
    // JSON object encoding with recursion
    throw UnimplementedError();
  }
}

class JsonName implements JsonElement {
  JsonName(this.value);
  final String value;

  @override
  String encode() {
    return '"$value"';
  }
}

abstract interface class JsonElement {
  String encode();
}

I'd be happy to contribute code to the work 🤝

from sdk.

Related Issues (20)

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.