Git Product home page Git Product logo

Comments (3)

dcodeIO avatar dcodeIO commented on September 23, 2024

Seems that it currently returns the long's internal state, which are the low 32 and the high 32 bits which in turn make up its 64 bit value. The third field indicates whether the long is to be interpreted as unsigned or not (signed). Unfortunately, a string conversion would lose the signed information.

Imho this should be handled by the developer, may it be adding the desired toJSON implementation to Long.prototype or something else which transparently calls toString.

from long.js.

PaulBGD avatar PaulBGD commented on September 23, 2024

I'm handling it myself as you suggested, but I did expect it to return the value as a string. One example of this is how Date handles it:

> new Date().toJSON()
'2016-06-28T23:59:07.147Z'

from long.js.

skortchmark9 avatar skortchmark9 commented on September 23, 2024

Seems like something more useful would be a JSON decoder which can deconstruct the object representation. You'd need to hack the global JSON.parse implementation a bit so it should be opt-in. Something like:

Long.parseJSONLongs = () => {
    const oldJSONparse = JSON.parse;
    JSON.parse = (json, transform=(x => x)) => {
	return oldJSONparse(json, (x) => {
		if ('low' in x && 'high' in x && 'unsigned' in x) {
			return transform(new Long(x.low, x.high, x.unsigned));
		} else {
			return transform(x);
		}
	});
    };
}

from long.js.

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.