Skip to content

Commit

Permalink
Add JSON encoding and decoding functions
Browse files Browse the repository at this point in the history
Include `std::json::encode` and `std::json::decode` in the standard library.
  • Loading branch information
apmasell authored and avarsava committed Jan 26, 2024
1 parent 8f8a554 commit 81e6910
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions changes/add_json_func.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Add `std::json::encode` and `std::json::decode` functions
7 changes: 7 additions & 0 deletions shesmu-server-ui/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ export function dateStartOfDay(date: Date): Date {
Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate())
);
}
export function decodeJson(input: string): any | null {
try {
return JSON.parse(input);
} catch (e) {
return null;
}
}
export function distinct<T>(
input: T[],
compare: (left: T, right: T) => boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,22 @@ public Imyhat returnType() {
"$runtime.dictIterator(%s)",
Imyhat.JSON,
new FunctionParameter("Map to encode", Imyhat.dictionary(Imyhat.STRING, Imyhat.JSON))),
FunctionDefinition.staticMethod(
String.join(Parser.NAMESPACE_SEPARATOR, "std", "json", "encode"),
RuntimeSupport.class,
"encodeJson",
"Encodes JSON data as a string",
"JSON.stringify(%s)",
Imyhat.STRING,
new FunctionParameter("Data to encode", Imyhat.JSON)),
FunctionDefinition.staticMethod(
String.join(Parser.NAMESPACE_SEPARATOR, "std", "json", "decode"),
RuntimeSupport.class,
"decodeJson",
"Decodes JSON data from a string",
"$runtime.jsonDecode(%s)",
Imyhat.JSON.asOptional(),
new FunctionParameter("Data to decode", Imyhat.STRING)),
FunctionDefinition.virtualMethod(
String.join(Parser.NAMESPACE_SEPARATOR, "std", "path", "file"),
"getFileName",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,15 @@ public static <T> Tuple collect(Stream<T> items, Function<Stream<T>, Object>...
return new Tuple(Stream.of(processors).map(p -> p.apply(data.stream())).toArray());
}

@RuntimeInterop
public static Optional<JsonNode> decodeJson(String input) {
try {
return Optional.of(MAPPER.readTree(input));
} catch (JsonProcessingException e) {
return Optional.empty();
}
}

/** Determine the difference between two instants, in seconds. */
@RuntimeInterop
public static long difference(Instant left, Instant right) {
Expand All @@ -167,6 +176,15 @@ public static <T> Set<T> difference(Imyhat type, Set<T> left, Set<T> right) {
return result;
}

@RuntimeInterop
public static String encodeJson(JsonNode input) {
try {
return MAPPER.writeValueAsString(input);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}

/**
* Group items in to outer groups, then apply a complex subgrouping operation to produce subgroups
* from this input
Expand Down

0 comments on commit 81e6910

Please sign in to comment.