Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Stringify expressions #10714

Closed
1ec5 opened this issue Dec 16, 2017 · 8 comments
Closed

Stringify expressions #10714

1ec5 opened this issue Dec 16, 2017 · 8 comments
Assignees
Labels
feature iOS Mapbox Maps SDK for iOS macOS Mapbox Maps SDK for macOS runtime styling

Comments

@1ec5
Copy link
Contributor

1ec5 commented Dec 16, 2017

mbgl should have a mechanism for converting an mbgl::style::expression::Expression to a JSON structure or string. Perhaps it could live in stringify.hpp. This would be useful for implementing the style property getters on iOS/macOS for #8074: instead of working with the specialized C++ types, the getter code could transform JSON-like structures, just as the setter code produces JSON-like structures via a converter.

/ref #7563
/cc @anandthakker

@1ec5
Copy link
Contributor Author

1ec5 commented Jan 4, 2018

This mechanism or one that converts to mbgl::Value would also enable the SDKs to expose alternative setters and getters in JSON format. Studio just added an “Edit property as JSON” option that these setters and getters could tie into, and React Native Mapbox GL would also benefit from not having to reverse-engineer the native SDKs’ expression APIs.

On the iOS/macOS side, we’re still going to prefer the NSExpression syntax, as it would require far fewer @s in code, but an +[NSExpression mgl_expressionForJSONObject:] and -[NSExpression mgl_jsonObject] would be a sensible alternative for certain workflows.

/cc @nitaliano @davidtheclark

@anandthakker
Copy link
Contributor

👍 this shouldn't be too hard to add.

Perhaps it could live in stringify.hpp

Might be easier for this to be a member function on each mbgl::style::expression::Expression subclass.

@1ec5 1ec5 added this to the ios-v4.0.0 milestone Jan 18, 2018
@ChrisLoer ChrisLoer self-assigned this Feb 7, 2018
ChrisLoer added a commit that referenced this issue Feb 16, 2018
Issue #10714
- Each expression stores its operator as a string, and default serialization is [operator, serialize(child1), ...]
- Custom implementations of `serialize` for Expression types that don't follow the pattern
- expression::Value -> mbgl::Value converter
- node_expression bindings to expose `serialize`
ChrisLoer added a commit that referenced this issue Feb 16, 2018
Issue #10714
- Each expression stores its operator as a string, and default serialization is [operator, serialize(child1), ...]
- Custom implementations of `serialize` for Expression types that don't follow the pattern
- expression::Value -> mbgl::Value converter
- node_expression bindings to expose `serialize`
@ChrisLoer
Copy link
Contributor

Implemented in #11156.

@nitaliano
Copy link
Contributor

@ChrisLoer @1ec5 so will this be exposed in the Android/iOS SDK so from react native I can just send a JSON blob?

@ChrisLoer
Copy link
Contributor

I'm not sure on the Android side -- do you know how we'll expose it @tobrun? On the iOS side there is currently MGLJSONObjectFromMBGLExpression, which @1ec5 plans to re-implement as a thin wrapper on this functionality. I take it react-native-mapbox-gl is implemented on top of the iOS/Android SDK interface so it can't call directly into mbgl-core code?

@nitaliano
Copy link
Contributor

yes that is correct

@1ec5
Copy link
Contributor Author

1ec5 commented Feb 20, 2018

@nitaliano, as of #11072, the iOS map SDK already exposes two methods for roundtripping between NSExpression and JSON-style Foundation objects (like NSArray). The MGLJSONObjectFromMBGLExpression() refactoring in #11254 plus these issues will fix a number of roundtripping bugs, but there are no plans to allow e.g. MGLSymbolStyleLayer.text to be set to a JSON object directly, since the paint and layout properties are declared with the type NSExpression.

@tobrun
Copy link
Member

tobrun commented Feb 20, 2018

@nitaliano

Input

#11158 was ticketed out to support that use-case.

Output

You can use Expression.toString() that will generate a json string that is compliant to the syntax form the style spec.

eg.

  @Test
  public void testExpressionMinToString() throws Exception {
    String expected = "[\"min\", 0, 1, 2, 3]";
    String actual = min(0, 1, 2, 3).toString();
    assertEquals("toString should match", expected, actual);
  }

Note that I'm still working on the getters support for this in #10721

tobrun pushed a commit that referenced this issue Feb 26, 2018
Issue #10714
- Each expression stores its operator as a string, and default serialization is [operator, serialize(child1), ...]
- Custom implementations of `serialize` for Expression types that don't follow the pattern
- expression::Value -> mbgl::Value converter
- node_expression bindings to expose `serialize`
jfirebaugh pushed a commit that referenced this issue Feb 28, 2018
Issue #10714
- Each expression stores its operator as a string, and default serialization is [operator, serialize(child1), ...]
- Custom implementations of `serialize` for Expression types that don't follow the pattern
- expression::Value -> mbgl::Value converter
- node_expression bindings to expose `serialize`
jfirebaugh pushed a commit that referenced this issue Mar 1, 2018
Issue #10714
- Each expression stores its operator as a string, and default serialization is [operator, serialize(child1), ...]
- Custom implementations of `serialize` for Expression types that don't follow the pattern
- expression::Value -> mbgl::Value converter
- node_expression bindings to expose `serialize`
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature iOS Mapbox Maps SDK for iOS macOS Mapbox Maps SDK for macOS runtime styling
Projects
None yet
Development

No branches or pull requests

5 participants