Skip to content

Commit

Permalink
Normalize JSON5 upsert as text upsert to properly replicate JSON5 con…
Browse files Browse the repository at this point in the history
…tent
  • Loading branch information
ks-yim committed Feb 2, 2022
1 parent e0f6fbb commit a3c3e64
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ static Change<String> ofTextUpsert(String path, byte[] content) {
static Change<String> ofTextUpsert(String path, String text) {
requireNonNull(text, "text");
validateFilePath(path, "path");
if (EntryType.guessFromPath(path) == EntryType.JSON) {
if (EntryType.guessFromPath(path) == EntryType.JSON && !isJson5(path)) {
throw new ChangeFormatException("invalid file type: " + path +
" (expected: a non-JSON file). Use Change.ofJsonUpsert() instead");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
import static java.util.Objects.requireNonNull;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,7 @@ private Map<String, Change<?>> toChangeMap(List<DiffEntry> diffEntryList) {
switch (diffEntry.getChangeType()) {
case MODIFY:
// Resolve JSON5 as EntryType.TEXT to modify it with text patch because
// json patch can hardly be applied to JSON5.
// applying json patch to JSON5 file makes it a plain JSON.
final EntryType oldEntryType = !isJson5(oldPath) ? EntryType.guessFromPath(oldPath)
: EntryType.TEXT;
switch (oldEntryType) {
Expand Down Expand Up @@ -798,7 +798,10 @@ private Map<String, Change<?>> toChangeMap(List<DiffEntry> diffEntryList) {
}
break;
case ADD:
final EntryType newEntryType = EntryType.guessFromPath(newPath);
// Resolve JSON5 as EntryType.TEXT so that the JSON5 content can properly be included
// in the serialized ReplicationLog.
final EntryType newEntryType = !isJson5(newPath) ? EntryType.guessFromPath(newPath)
: EntryType.TEXT;
switch (newEntryType) {
case JSON: {
final String jsonText = new String(
Expand Down

0 comments on commit a3c3e64

Please sign in to comment.