Skip to content

Commit 0286eac

Browse files
authored
Merge pull request #51 from filip26/patch/java-adapter-api
Add method to process native Map representing JSON object
2 parents 254763d + ecb5d78 commit 0286eac

3 files changed

Lines changed: 62 additions & 8 deletions

File tree

README.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
# Titanium JCS
22

3-
> ⚠️ **Warning** JCS is flawed and lacks true portability. By forcing other languages to emulate ECMAScript’s number handling, it creates fragile, incompatible implementations. Use it only if explicitly required.
3+
4+
[![Java 21 CI](https://github.com/filip26/titanium-jcs/actions/workflows/java21-build.yml/badge.svg)](https://github.com/filip26/titanium-jcs/actions/workflows/java21-build.yml)
5+
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/af8879b14a3f45bd8205c7720a24612f)](https://app.codacy.com/gh/filip26/titanium-jcs/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)
6+
[![Codacy Badge](https://app.codacy.com/project/badge/Coverage/af8879b14a3f45bd8205c7720a24612f)](https://app.codacy.com/gh/filip26/titanium-jcs/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_coverage)
7+
[![javadoc](https://javadoc.io/badge2/com.apicatalog/titanium-jcs/javadoc.svg)](https://javadoc.io/doc/com.apicatalog/titanium-jcs)
8+
[![Maven Central](https://img.shields.io/maven-central/v/com.apicatalog/titanium-jcs.svg?label=Maven%20Central)](https://search.maven.org/search?q=g:com.apicatalog%20AND%20a:titanium-jcs)
9+
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
10+
411

512
The JSON Canonicalization Scheme (JCS) defines a deterministic way to serialize JSON so that the same JSON data model always produces the same byte sequence.
613

@@ -10,12 +17,8 @@ By normalizing JSON into a canonical form, JCS ensures interoperability across s
1017

1118
Titanium JCS is a Java implementation of the [RFC 8785 JSON Canonicalization Scheme (JCS)](https://www.rfc-editor.org/rfc/rfc8785).
1219

13-
[![Java 21 CI](https://github.com/filip26/titanium-jcs/actions/workflows/java21-build.yml/badge.svg)](https://github.com/filip26/titanium-jcs/actions/workflows/java21-build.yml)
14-
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/af8879b14a3f45bd8205c7720a24612f)](https://app.codacy.com/gh/filip26/titanium-jcs/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)
15-
[![Codacy Badge](https://app.codacy.com/project/badge/Coverage/af8879b14a3f45bd8205c7720a24612f)](https://app.codacy.com/gh/filip26/titanium-jcs/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_coverage)
16-
[![javadoc](https://javadoc.io/badge2/com.apicatalog/titanium-jcs/javadoc.svg)](https://javadoc.io/doc/com.apicatalog/titanium-jcs)
17-
[![Maven Central](https://img.shields.io/maven-central/v/com.apicatalog/titanium-jcs.svg?label=Maven%20Central)](https://search.maven.org/search?q=g:com.apicatalog%20AND%20a:titanium-jcs)
18-
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
20+
> 📝 Note: Handling decimals in JSON is a known challenge, and certain corner cases may not behave as expected. JCS adheres to ECMAScript’s number handling, which can reflect these limitations with decimal values.
21+
1922

2023
## ✨ Features
2124

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<modelVersion>4.0.0</modelVersion>
66
<groupId>com.apicatalog</groupId>
77
<artifactId>titanium-jcs</artifactId>
8-
<version>3.0.0-M2</version>
8+
<version>3.0.0-M3</version>
99
<packaging>jar</packaging>
1010

1111
<url>https://github.com/filip26/titanium-jcs</url>

src/main/java/com/apicatalog/jcs/Jcs.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@
2222
import java.text.DecimalFormat;
2323
import java.text.DecimalFormatSymbols;
2424
import java.util.Locale;
25+
import java.util.Map;
2526

2627
import com.apicatalog.tree.io.Tree.NodeType;
2728
import com.apicatalog.tree.io.TreeAdapter;
2829
import com.apicatalog.tree.io.TreeComparison;
2930
import com.apicatalog.tree.io.TreeIOException;
31+
import com.apicatalog.tree.io.java.JavaAdapter;
3032

3133
/**
3234
* An implementation of the <a href="https://www.rfc-editor.org/rfc/rfc8785">RFC
@@ -76,6 +78,19 @@ public final class Jcs {
7678
private static final DecimalFormat PLAIN_FORMAT = new DecimalFormat("0.#####################",
7779
new DecimalFormatSymbols(Locale.ENGLISH));
7880

81+
/**
82+
* Canonicalizes as JSON object according to JCS (RFC 8785) and returns the
83+
* result as a {@link String}.
84+
*
85+
* @param value the Map representing JSON object to canonicalize (can be
86+
* {@code null})
87+
* @return a string containing the canonical JSON representation
88+
* @throws TreeIOException
89+
*/
90+
public static String canonize(final Map<String, Object> value) throws TreeIOException {
91+
return canonize(value, JavaAdapter.instance());
92+
}
93+
7994
/**
8095
* Canonicalizes a JSON value according to JCS (RFC 8785) and returns the result
8196
* as a {@link String}.
@@ -97,6 +112,21 @@ public static String canonize(final Object value, final TreeAdapter adapter) thr
97112
}
98113
}
99114

115+
/**
116+
* Canonicalizes {@link Map} representing JSON object according to JCS (RFC
117+
* 8785) and writes the output to the provided {@link Writer}.
118+
*
119+
* @param value the {@link Map} representing JSON object to canonicalize (can
120+
* be {@code null})
121+
* @param writer the {@link Writer} to which the canonical output is written
122+
* @throws IOException if an I/O error occurs
123+
* @throws TreeIOException
124+
*/
125+
public static void canonize(final Map<String, Object> value, final Writer writer)
126+
throws IOException, TreeIOException {
127+
canonize(value, JavaAdapter.instance(), writer);
128+
}
129+
100130
/**
101131
* Canonicalizes a JSON value according to JCS (RFC 8785) and writes the output
102132
* to the provided {@link Writer}.
@@ -116,6 +146,27 @@ public static void canonize(final Object value, final TreeAdapter adapter, final
116146
(new JcsGenerator(writer)).node(value, adapter);
117147
}
118148

149+
/**
150+
* Compares two {@link Map} representing JSON objects for canonical equality
151+
* under JCS (RFC 8785).
152+
*
153+
* <p>
154+
* Two JSON values are canonically equal if their data models are equivalent.
155+
* This involves comparing numbers by their canonical string representation and
156+
* objects by their members, sorted lexicographically by key.
157+
* </p>
158+
*
159+
* @param value1 the first {@link Map} representing JSON object to compare (can
160+
* be {@code null})
161+
* @param value2 the second {@link Map} representing JSON object to compare (can
162+
* be {@code null})
163+
* @return {@code true} if the values are canonically equal, {@code false}
164+
* otherwise
165+
*/
166+
public static boolean equals(final Map<String, Object> value1, final Map<String, Object> value2) {
167+
return equals(value1, value2, JavaAdapter.instance());
168+
}
169+
119170
/**
120171
* Compares two JSON values for canonical equality under JCS (RFC 8785).
121172
*

0 commit comments

Comments
 (0)