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

Add 'to-json' expression #7796

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/components/expression-metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ const types = {
type: 'color',
parameters: ['value', { repeat: [ 'fallback: value' ] }]
}],
'to-json': [{
type: 'json',
parameters: ['string']
}],
'to-number': [{
type: 'number',
parameters: ['value', { repeat: [ 'fallback: value' ] }]
Expand Down
12 changes: 10 additions & 2 deletions src/style-spec/expression/definitions/coercion.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import assert from 'assert';

import {BooleanType, ColorType, NumberType, StringType, ValueType} from '../types';
import {BooleanType, ColorType, JsonType, NumberType, StringType, ValueType} from '../types';
import {Color, toString as valueToString, validateRGBA} from '../values';
import RuntimeError from '../runtime_error';
import Formatted from '../types/formatted';
Expand All @@ -17,6 +17,7 @@ import type { Type } from '../types';
const types = {
'to-boolean': BooleanType,
'to-color': ColorType,
'to-json': JsonType,
'to-number': NumberType,
'to-string': StringType
};
Expand Down Expand Up @@ -44,7 +45,7 @@ class Coercion implements Expression {
const name: string = (args[0]: any);
assert(types[name], name);

if ((name === 'to-boolean' || name === 'to-string') && args.length !== 2)
if ((name === 'to-boolean' || name === 'to-string' || name === 'to-json') && args.length !== 2)
return context.error(`Expected one argument.`);

const type = types[name];
Expand Down Expand Up @@ -99,6 +100,13 @@ class Coercion implements Expression {
// There is no explicit 'to-formatted' but this coercion can be implicitly
// created by properties that expect the 'formatted' type.
return Formatted.fromString(valueToString(this.args[0].evaluate(ctx)));
} else if (this.type.kind === 'json') {
const input = this.args[0].evaluate(ctx);
try {
return JSON.parse(input);
} catch (error) {
throw new RuntimeError(error || `Could not parse JSON from value '${input}'`);
}
} else {
return valueToString(this.args[0].evaluate(ctx));
}
Expand Down
1 change: 1 addition & 0 deletions src/style-spec/expression/definitions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ const expressions: ExpressionRegistry = {
'string': Assertion,
'to-boolean': Coercion,
'to-color': Coercion,
'to-json': Coercion,
'to-number': Coercion,
'to-string': Coercion,
'var': Var
Expand Down
4 changes: 4 additions & 0 deletions src/style-spec/expression/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export type NumberTypeT = { kind: 'number' };
export type StringTypeT = { kind: 'string' };
export type BooleanTypeT = { kind: 'boolean' };
export type ColorTypeT = { kind: 'color' };
export type JsonTypeT = { kind: 'json' };
export type ObjectTypeT = { kind: 'object' };
export type ValueTypeT = { kind: 'value' };
export type ErrorTypeT = { kind: 'error' };
Expand All @@ -19,6 +20,7 @@ export type Type =
StringTypeT |
BooleanTypeT |
ColorTypeT |
JsonTypeT |
ObjectTypeT |
ValueTypeT |
ArrayType | // eslint-disable-line no-use-before-define
Expand All @@ -37,6 +39,7 @@ export const NumberType = { kind: 'number' };
export const StringType = { kind: 'string' };
export const BooleanType = { kind: 'boolean' };
export const ColorType = { kind: 'color' };
export const JsonType = { kind: 'json' };
export const ObjectType = { kind: 'object' };
export const ValueType = { kind: 'value' };
export const ErrorType = { kind: 'error' };
Expand Down Expand Up @@ -68,6 +71,7 @@ const valueMemberTypes = [
StringType,
BooleanType,
ColorType,
JsonType,
FormattedType,
ObjectType,
array(ValueType)
Expand Down
9 changes: 9 additions & 0 deletions src/style-spec/reference/v8.json
Original file line number Diff line number Diff line change
Expand Up @@ -2606,6 +2606,15 @@
}
}
},
"to-json": {
"doc": "Converts the input value to a JSON. The input is converted to a JSON using the [`JSON.parse`](https://tc39.github.io/ecma262/#sec-json.parse) function of the ECMAScript Language Specification.",
"group": "Types",
"sdk-support": {
"basic functionality": {
"js": "0.52.0"
}
}
},
"to-string": {
"doc": "Converts the input value to a string. If the input is `null`, the result is `\"\"`. If the input is a boolean, the result is `\"true\"` or `\"false\"`. If the input is a number, it is converted to a string as specified by the [\"NumberToString\" algorithm](https://tc39.github.io/ecma262/#sec-tostring-applied-to-the-number-type) of the ECMAScript Language Specification. If the input is a color, it is converted to a string of the form `\"rgba(r,g,b,a)\"`, where `r`, `g`, and `b` are numerals ranging from 0 to 255, and `a` ranges from 0 to 1. Otherwise, the input is converted to a string in the format specified by the [`JSON.stringify`](https://tc39.github.io/ecma262/#sec-json.stringify) function of the ECMAScript Language Specification.",
"group": "Types",
Expand Down
10 changes: 10 additions & 0 deletions test/integration/expression-tests/to-json/2-ary/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"expression": ["to-json", ["get", "x"], ["get", "y"]],
"inputs": [[{}, {}]],
"expected": {
"compiled": {
"result": "error",
"errors": [{"key": "", "error": "Expected one argument."}]
}
}
}
21 changes: 21 additions & 0 deletions test/integration/expression-tests/to-json/basic/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"expression": ["to-json", ["get", "x"]],
"inputs": [
[{}, {"properties": {"x": "1"}}],
[{}, {"properties": {"x": "false"}}],
[{}, {"properties": {"x": "null"}}],
[{}, {"properties": {"x": "string"}}],
[{}, {"properties": {"x": "[1, 2]"}}],
[{}, {"properties": {"x": "{\"y\":1}"}}]
],
"expected": {
"compiled": {
"result": "success",
"isFeatureConstant": false,
"isZoomConstant": true,
"type": "json"
},
"outputs": [1, false, null, {"error":{}}, [1, 2], {"y": 1}],
"serialized": ["to-json", ["get", "x"]]
}
}