Skip to content

Conversation

@muditchaudhary
Copy link
Contributor

Overview

Implements JSON conversion support for CedarJava, allowing PolicySet objects to be converted to Cedar's JSON format, similar to the to_json() functionality in the cedar_policy Rust library.

Changes

  • Adds .toJson method for PolicySet to convert PolicySet to Cedar JSON format
  • Adds tests for both Java and FFI
  • Improves test output clarity by adding env.exception_clear().unwrap()

Example Usage

EntityTypeName principalType = EntityTypeName.parse("User").get();
Set<Policy> policies = new HashSet<>();
Set<Policy> templates = new HashSet<>();
ArrayList<TemplateLink> templateLinks = new ArrayList<TemplateLink>();
ArrayList<LinkValue> linkValueList = new ArrayList<>();

String fullPolicy =
        "permit(principal == User::\"Bob\", action == Action::\"View_Photo\", resource in Album::\"Vacation\");";
Policy newPolicy = new Policy(fullPolicy, "p1");
policies.add(newPolicy);

String template = "permit(principal == ?principal, action == Action::\"View_Photo\", resource in Album::\"Vacation\");";
Policy policyTemplate = new Policy(template, "t0");
templates.add(policyTemplate);

Entity principal = new Entity(principalType.of("Alice"), new HashMap<>(), new HashSet<>());
LinkValue principalLinkValue = new LinkValue("?principal", principal.getEUID());
linkValueList.add(principalLinkValue);

TemplateLink templateLink = new TemplateLink("t0", "tl0", linkValueList);
templateLinks.add(templateLink);

PolicySet validPolicySet = PolicySet(policies, templates, templateLinks)

String validPolicySetJson = validPolicySet.toJson()

Signed-off-by: Mudit Chaudhary <chmudit@amazon.com>
…jectMapper

Signed-off-by: Mudit Chaudhary <chmudit@amazon.com>
Signed-off-by: Mudit Chaudhary <chmudit@amazon.com>
Signed-off-by: Mudit Chaudhary <chmudit@amazon.com>
Signed-off-by: Mudit Chaudhary <chmudit@amazon.com>
Signed-off-by: Mudit Chaudhary <chmudit@amazon.com>
Signed-off-by: Mudit Chaudhary <chmudit@amazon.com>
…ad of ObjectMapper"

This reverts commit 522f56d.

Signed-off-by: Mudit Chaudhary <chmudit@amazon.com>
Signed-off-by: Mudit Chaudhary <chmudit@amazon.com>
@muditchaudhary muditchaudhary marked this pull request as ready for review August 14, 2025 20:45
@muditchaudhary muditchaudhary merged commit 80688e3 into cedar-policy:main Aug 15, 2025
4 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.

3 participants