Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package de.jcup.hijson.document;

public class CommentEntry {

private String comment;

private int line;
private int column;

public int getLine() {
return line;
}

public void setLine(int line) {
this.line = line;
}

public int getColumn() {
return column;
}

public void setColumn(int column) {
this.column = column;
}

public void setComment(String comment) {
this.comment=comment;
}

public String getComment() {
return comment;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package de.jcup.hijson.document;

import java.util.ArrayList;
import java.util.List;

public class CommentHistory {


List<CommentEntry> list = new ArrayList<>();

public void addComment(int line, int column, String comment) {
CommentEntry entry = new CommentEntry();
entry.setComment(comment);
entry.setLine(line);
entry.setColumn(column);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package de.jcup.hijson.document;

public class CommentHistorySupport {

private SimpleJsonFormatter simpleFormatter = new SimpleJsonFormatter();

public CommentHistory ceateCommentHistory(String json) {
CommentHistory history = new CommentHistory();
/* FIXME de-jcup , 2023: handle comments hwich are in own line special: must be removed for line handling
* means simple formatter must have two ways of formatting... first the new line comments are contained,
* afterwards remove those as well to get the target line numbers... */
String jsonWithoutEmptyNewLines= simpleFormatter.formatJson(json);
String[] lines = jsonWithoutEmptyNewLines.split("\n");

for (int lineNumber=0;lineNumber<lines.length;lineNumber++) {
String line = lines[lineNumber].trim();
int column = line.indexOf("//");
if (column!=-1) {
history.addComment(lineNumber, column, line.substring(column));
}
}

return history;
}

public String applyCommentsToFormattedJson(String json, CommentHistory history) {
StringBuilder sb=new StringBuilder();
String[] lines = json.split("\n");
for (int lineNumber=0;lineNumber<lines.length;lineNumber++) {
String line = lines[lineNumber];
/* FIXME de-jcup , 2023: implement add of comments which are NOT at the beginning */
/* FIXME de-jcup , 2023: implement add of comments which ARE at the beginning - means we got a special numbering mechanism here*/
sb.append(line).append('\n');
}
return sb.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class JSONFormatSupport {
private static String STRING_COLONS_NO_WHITESPACES_BEFORE_QUOTE=":\"";
private static Pattern PATTERN_COLONS_NO_WHITESPACES_BEFORE_QUOTE= Pattern.compile(STRING_COLONS_NO_WHITESPACES_BEFORE_QUOTE);


public class FormatterResult {
private String origin;
private String formatted;
Expand Down Expand Up @@ -69,7 +70,7 @@ public FormatterResult(String origin, String formatted, FormatterResultState sta
}

}

public enum FormatterResultState {

KEPT_AS_IS,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package de.jcup.hijson.document;

import java.util.regex.Pattern;

public class SimpleJsonFormatter {

private static String STRING_PATTERN_SINGLE_LINE_REDUCE="(?m)^[ \t]*\r?\n";
private static Pattern PATTERN_SINGLE_LINE_REDUCE = Pattern.compile(STRING_PATTERN_SINGLE_LINE_REDUCE);

public String formatJson(String json) {
String formatted = json;
formatted= PATTERN_SINGLE_LINE_REDUCE.matcher(formatted).replaceAll("");
return formatted;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.core.json.JsonReadFeature;
import com.fasterxml.jackson.databind.JsonNode;

import de.jcup.hijson.outline.Item;
import de.jcup.hijson.outline.ItemType;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package de.jcup.hijson.document;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;

public class SimpleJsonFormatterTest {

private SimpleJsonFormatter formatterToTest;

@Before
public void before() {
formatterToTest = new SimpleJsonFormatter();
}

@Test
public void formatJson_reduces_lines() {
/* prepare */
/* @formatter:off */
String json = "{ // comment 0 is here\n" +
" \n" +
" // comment 1 is here\n" +
" \n" +
" \"a\" : \"value1\",\n" +
" \n" +
" \n" +
" \"b\" : \"value2\", // comment2 is here\n" +
" \"c\" : \"value3\"\n" +
" \n" +
" \n" +
" }";


/* execute */
String formatted = formatterToTest.formatJson(json);

/* test */
String expected = "{ // comment 0 is here\n" +
" // comment 1 is here\n" +
" \"a\" : \"value1\",\n" +
" \"b\" : \"value2\", // comment2 is here\n" +
" \"c\" : \"value3\"\n" +
" }";
/* @formatter:on */

assertEquals(expected,formatted );
}

}