Skip to content

Commit ac4f3af

Browse files
authored
Merge pull request #142 from filip26/issue/125
Improve Noncompressible Document Error (#125)
2 parents 2352365 + 5e0287a commit ac4f3af

File tree

5 files changed

+40
-13
lines changed

5 files changed

+40
-13
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ var encoder = CborLd.createEncoder()
4646
byte[] encoded = encoder.encode(document);
4747
```
4848

49+
> Please note: Only JSON-LD documents containing referenced contexts can be compressed. This is because referenced contexts act as shared dictionaries that enable term mapping across documents. Compression is not possible with inline contexts, as their definitions are embedded directly in the document and cannot be reused or referenced externally.
50+
51+
4952
### Decoding
5053

5154
```javascript

src/main/java/com/apicatalog/cborld/encoder/DefaultEncoder.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,17 @@ public final byte[] encode(JsonObject document) throws EncoderException, Context
7474

7575
return compress(document, contexts);
7676

77-
// non compressable context
77+
// non compressible context
7878
} catch (IllegalArgumentException e) {
7979
/* ignored, expected in a case of non compress-able documents */
8080
}
8181

82-
throw new EncoderException(Code.InvalidDocument, "Non compress-able document.");
82+
throw new EncoderException(
83+
Code.NonCompressible,
84+
"""
85+
Non-compressible document. Only JSON-LD documents containing referenced contexts can be compressed. \
86+
Referenced contexts serve as a shared dictionary, which is not possible with inline contexts.
87+
""");
8388
}
8489

8590
/**
@@ -101,11 +106,11 @@ final byte[] compress(final JsonObject document, Collection<String> contextUrls)
101106
try {
102107
// 2.CBOR Tag - 0xD9
103108
baos.write(CborLd.LEADING_BYTE);
104-
109+
105110
final CborBuilder builder = new CborBuilder();
106-
111+
107112
MapBuilder<?> mapBuilder;
108-
113+
109114
switch (config.version()) {
110115
case V1:
111116
baos.write(CborLd.VERSION_1_BYTES[0]);
@@ -120,7 +125,7 @@ final byte[] compress(final JsonObject document, Collection<String> contextUrls)
120125
baos.write(config.dictionary().code());
121126
mapBuilder = builder.addMap();
122127
break;
123-
128+
124129
case V05:
125130
baos.write(CborLd.VERSION_05_BYTE);
126131
baos.write(config.dictionary().code());

src/main/java/com/apicatalog/cborld/encoder/EncoderContext.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.LinkedHashSet;
55
import java.util.Map.Entry;
66

7+
import com.apicatalog.cborld.encoder.EncoderException.Code;
78
import com.apicatalog.jsonld.json.JsonUtils;
89
import com.apicatalog.jsonld.lang.Keywords;
910
import com.apicatalog.jsonld.uri.UriUtils;
@@ -15,11 +16,11 @@
1516

1617
class EncoderContext {
1718

18-
public final static Collection<String> get(final JsonObject document) {
19+
public final static Collection<String> get(final JsonObject document) throws EncoderException {
1920
return get(document, new LinkedHashSet<>());
2021
}
2122

22-
static final Collection<String> get(final JsonObject document, Collection<String> contexts) {
23+
static final Collection<String> get(final JsonObject document, Collection<String> contexts) throws EncoderException {
2324

2425
for (final Entry<String, JsonValue> entry : document.entrySet()) {
2526

@@ -33,7 +34,7 @@ static final Collection<String> get(final JsonObject document, Collection<String
3334
return contexts;
3435
}
3536

36-
static final void processContextValue(final JsonValue jsonValue, final Collection<String> result) {
37+
static final void processContextValue(final JsonValue jsonValue, final Collection<String> result) throws EncoderException {
3738

3839
if (JsonUtils.isString(jsonValue)) {
3940
final String uri = ((JsonString) jsonValue).getString();
@@ -63,6 +64,13 @@ static final void processContextValue(final JsonValue jsonValue, final Collectio
6364
}
6465
}
6566

66-
throw new IllegalArgumentException("Non compress-able context detected " + jsonValue + ".");
67+
// throw new IllegalArgumentException("Non compress-able context detected " + jsonValue + ".");
68+
throw new EncoderException(
69+
Code.NonCompressible,
70+
"""
71+
Non-compressible document. Only JSON-LD documents containing referenced contexts can be compressed. \
72+
Referenced contexts serve as a shared dictionary, which is not possible with inline contexts.
73+
""");
74+
6775
}
6876
}

src/main/java/com/apicatalog/cborld/encoder/EncoderException.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,20 @@ public class EncoderException extends Exception {
44

55
private static final long serialVersionUID = 4949655193741388758L;
66

7+
/**
8+
* Error codes indicating the reason for an {@link EncoderException}.
9+
*/
710
public enum Code {
8-
Internal, // internal - an unexpected error
9-
InvalidDocument, // invalid JSON-LD document
11+
/** An unexpected internal error occurred. */
12+
Internal,
13+
14+
/** The input JSON-LD document is invalid. */
15+
InvalidDocument,
16+
17+
/** The document cannot be compressed (e.g., it contains inline contexts). */
18+
NonCompressible,
19+
20+
/** The operation is not supported. */
1021
Unsupported,
1122
}
1223

src/test/resources/com/apicatalog/cborld/encoder-manifest.jsonld

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@
338338
],
339339
"name": "non compress-able document",
340340
"input": "encoder/0.5/0061-in.jsonld",
341-
"expectErrorCode": "InvalidDocument",
341+
"expectErrorCode": "NonCompressible",
342342
"option": {
343343
"config": "v05"
344344
}

0 commit comments

Comments
 (0)