Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider specializing a subset for JSON #25

Open
Yoric opened this issue Feb 22, 2018 · 4 comments
Open

Consider specializing a subset for JSON #25

Yoric opened this issue Feb 22, 2018 · 4 comments

Comments

@Yoric
Copy link
Collaborator

Yoric commented Feb 22, 2018

I believe that we should consider offering a subset of the syntax specialized for JSON-style literal expressions.

Expected benefits

  • combined with Skippable, we could skip over these literals and read the object lazily;
  • one more optimization lever for the encoder when it meets JSON-style literals;
  • this can be used straightforwardly to extend JSON.parse() to TypeArray and create a new JSON.binify();
  • it is possible (but not certain) that we could make this encoding more concise and faster to decode.

Possible spec

typedef (... // Previous stuff
         LiteralExpression)
        Expression;

typedef (LiteralObjectExpression or
         LiteralBooleanExpression or
         LiteralStringExpression or
         LiteralNullExpression or
         LiteralNumericExpression or
         LiteralArrayExpression)
        LiteralExpression;

typedef (EagerLiteralObjectExpression or SkippableLiteralObjectExpression) LiteralObjectExpression;
typedef (EagerLiteralArrayExpression or SkippableLiteralArrayExpression) LiteralArrayExpression;

interface EagerLiteralObjectExpression {
    attribute FrozenArray<LiteralObjectProperty> properties;
}
[Skippable] interface SkippableLiteralObjectExpression {
    attribute EagerLiteralObjectExpression value;
}

interface LiteralObjectProperty {
   attribute LiteralPropertyName name;
   attribute LiteralExpression value;
}
interface EagerLiteralArrayExpression {
   attribute FrozenArray<LiteralExpression> elements;
}
[Skippable] interface SkippableLiteralArrayExpression {
    attribute EagerLiteralArrayExpression value;
}
@ljharb
Copy link
Member

ljharb commented Feb 22, 2018

Since object literals can contain both non-JSON-representable types, and because it can contain any arbitrary type that has a .toJSON method, what would be the advantage to trying to represent a “JSON-style” literal?

@Yoric
Copy link
Collaborator Author

Yoric commented Feb 23, 2018

For the moment, it's more of an intuition than anything else. But my intuition tells me that we should be able to make things faster and more efficient if we know that we're dealing with a very small subset of JavaScript that can only contain pure data, and in particular nothing unsafe (assuming that it doesn't throw a SyntaxError).

@ljharb
Copy link
Member

ljharb commented Feb 23, 2018

That sounds nice, but I’m not sure how those things can be statically known.

@dead-claudia
Copy link

@Yoric Take a look at BSON (used in MongoDB) and CBOR (used in some memory-sensitive cases). They both are very highly inspired by JSON, up to and including a default schema-less representation and a relatively small-ish set of primitive types.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants