Skip to content

Conversation

rnewton-bp3
Copy link
Contributor

Description

This PR adds a new built-in FEEL function to Json(object): string that performs the inverse of the existing from Json(json):any function. It allows FEEL expressions to serialize values (contexts, lists, primitives, temporal values, and durations) into JSON strings, similar to JSON.stringify(any) function in JavaScript. This is especially useful for integration scenarios where FEEL values need to be passed to systems expecting JSON.

Implementation details:

Introduced helper method convertToJsonValue(Val) to recursively map FEEL Val types into JSON-compatible Java/Scala values.

Supports all FEEL data types, including temporal values (ValDate, ValTime, ValDateTime, ValLocalTime, ValLocalDateTime) formatted using ISO-8601 standards.

Durations are serialized using their toString representation.

The resulting JSON string is wrapped in a ValString to be fully FEEL-compatible.

Added safety net to return ValError when serialization fails (though in practice all FEEL types are supported).

I could not create a meaningful test that triggers the ValError(s"Failed to convert object to JSON: ${obj.getClass.getSimpleName}") case because all FEEL data types are fully mapped in the convertToJsonValue(Val) function. Triggering this branch would require artificially passing in unsupported or non-FEEL types, which isn’t a real usage scenario.

Finally, I’m a complete Scala noob, so I appreciate any suggestions for making the code cleaner or more idiomatic.

Thanks to Alex Trukhny (@sashtran) 🙏 for the initial review.

Related issues
closes #602

This PR adds a new `to json(any): string` as the inverse of `from Json(json)` built-in conversion function.
Converts FEEL values (contexts, lists, primitives, temporal values, durations) into JSON strings.

Uses convertToJsonValue helper to recursively map FEEL types to JSON-compatible Java/Scala types, with temporal values formatted as ISO-8601.

Returns ValString containing the serialized JSON, or ValError if serialization fails.
Note: All FEEL types are currently supported, so the error branch cannot be triggered in normal usage.
@rnewton-bp3 rnewton-bp3 requested a review from saig0 as a code owner August 19, 2025 19:13
@CLAassistant
Copy link

CLAassistant commented Aug 19, 2025

CLA assistant check
All committers have signed the CLA.

@saig0
Copy link
Member

saig0 commented Sep 9, 2025

@rnewton-bp3 thank you for your contribution. 🎉 I'll review your changes soon. 👀

@saig0 saig0 requested a review from Copilot September 9, 2025 08:39
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds a new built-in FEEL function to json(object): string that serializes FEEL values into JSON strings, providing the inverse functionality to the existing from json() function. This enables FEEL expressions to convert contexts, lists, primitives, temporal values, and durations into JSON format for integration scenarios.

  • Implements to json() function with support for all FEEL data types including temporal values and durations
  • Adds comprehensive test coverage for various data types and edge cases
  • Uses ISO-8601 formatting for temporal values and toString representation for durations

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/main/scala/org/camunda/feel/impl/builtin/ConversionBuiltinFunctions.scala Implements the to json() function with recursive value conversion logic
src/test/scala/org/camunda/feel/impl/builtin/BuiltinConversionFunctionsTest.scala Adds comprehensive test cases covering all supported data types

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Copy link
Member

@saig0 saig0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rnewton-bp3 looks good. 👍 I have a few comments to improve and complete the PR. Please have a look. 🍪

rnewton-bp3 and others added 2 commits September 9, 2025 10:07
- Remove duplicate tests
- Include ValFunction and ValRange conversions
@rnewton-bp3 rnewton-bp3 requested a review from saig0 September 11, 2025 02:05
Copy link
Member

@saig0 saig0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rnewton-bp3 looks very good. 👍

I have just one minor improvement. Please take a look. 🍰

@rnewton-bp3 rnewton-bp3 requested a review from saig0 October 3, 2025 16:56
Copy link
Member

@saig0 saig0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rnewton-bp3 looks great. 🎉

Thank you for contributing this valuable new feature. 🚀

@saig0 saig0 enabled auto-merge October 8, 2025 07:44
@saig0 saig0 added this pull request to the merge queue Oct 8, 2025
Merged via the queue into camunda:main with commit 3c0ddda Oct 8, 2025
5 checks passed
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

Successfully merging this pull request may close these issues.

Stringify JSON objects

4 participants