Skip to content

Commit

Permalink
Corrections with escape characters.
Browse files Browse the repository at this point in the history
  • Loading branch information
tvrzna committed Aug 5, 2022
1 parent 2889cc1 commit 5ec2c8a
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 6 deletions.
34 changes: 34 additions & 0 deletions src/main/java/cz/tvrzna/jackie/CommonUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,40 @@ public class CommonUtils
Number.class, Float.class, Double.class, Date.class, boolean.class, byte.class, char.class, short.class, int.class, long.class, float.class, double.class);
protected static final List<Class<?>> PRIMITIVE_CLASSES = Arrays.asList(boolean.class, byte.class, char.class, short.class, int.class, long.class, float.class, double.class);


/**
* The Enum ESCAPE_CHARACTERS.
*
* @author michalt
* @since 0.3.1
*/
protected enum ESCAPE_CHARACTERS {
TAB("\\t", "\t"),
CARRIAGE_RETURN("\\r", "\r"),
NEW_LINE("\\n", "\n"),
FORM_FEED("\\f", "\f"),
BACKSPACE("\\b", "\b");

private final String value;
private final String character;

private ESCAPE_CHARACTERS(String value, String character)
{
this.value = value;
this.character = character;
}

public String getValue()
{
return value;
}

public String getCharacter()
{
return character;
}
}

private CommonUtils()
{
}
Expand Down
16 changes: 15 additions & 1 deletion src/main/java/cz/tvrzna/jackie/Deserializator.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import java.util.List;
import java.util.Map;

import cz.tvrzna.jackie.CommonUtils.ESCAPE_CHARACTERS;

/**
* The Class Deserializator.
*
Expand Down Expand Up @@ -193,7 +195,19 @@ else if (delimiter != null && !previousWasEscape && (c == '\'' || c == '"'))
}
else
{
sw.write(c);
if (previousWasEscape && c != '"' && c != '\'')
{
String character = "\\" + Character.toString((char) c);
for (ESCAPE_CHARACTERS escape : ESCAPE_CHARACTERS.values())
{
character = character.replace(escape.getValue(), escape.getCharacter());
}
sw.write(character);
}
else
{
sw.write(c);
}
previousWasEscape = false;
}
}
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/cz/tvrzna/jackie/Serializator.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import java.util.Map.Entry;
import java.util.Optional;

import cz.tvrzna.jackie.CommonUtils.ESCAPE_CHARACTERS;

/**
* The Class Serializator.
*
Expand Down Expand Up @@ -76,7 +78,12 @@ private static String serializeValue(Object value, Config config)
}
else if (value instanceof String)
{
return getSeparator().concat(((String) value).replace(getSeparator(), "\\".concat(getSeparator()))).concat(getSeparator());
String data = getSeparator().concat(((String) value).replace(getSeparator(), "\\".concat(getSeparator()))).concat(getSeparator());
for (ESCAPE_CHARACTERS escape : ESCAPE_CHARACTERS.values())
{
data = data.replace(escape.getCharacter(), escape.getValue());
}
return data;
}
else if (value instanceof Number)
{
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/cz/tvrzna/jackie/JackieTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ public void fromJsonToHashMap()
@Test
public void fromJsonToObject()
{
String json = "{id: 20200327, 'name': \"SomeDopeName\", 'children': [{id: 1, name: 'help1', children: [{}]}, {id: 2, name: 'help2'}, {id: 3}], 'mapChildren': {'abc': {id: 4, name: 'help4', children: [{id: 5}]}, 'xyz': {id: 6}}}";
String json = "{id: 20200327, 'name': \"SomeDope\\n\\nName\", 'children': [{id: 1, name: 'help1', children: [{}]}, {id: 2, name: 'help2'}, {id: 3}], 'mapChildren': {'abc': {id: 4, name: 'help4', children: [{id: 5}]}, 'xyz': {id: 6}}}";
TestClass obj = new Jackie().fromJson(json, TestClass.class);
Assertions.assertNotNull(obj);
Assertions.assertEquals(20200327l, obj.id);
Assertions.assertEquals("SomeDopeName", obj.name);
Assertions.assertEquals("SomeDope\n\nName", obj.name);
Assertions.assertEquals(3, obj.children.size());
Assertions.assertNotNull(obj.children.get(2));
Assertions.assertEquals(6, obj.mapChildren.get("xyz").id);
Expand Down Expand Up @@ -113,10 +113,10 @@ public void toJsonFromChars()
@Test
public void toJsonFromObject()
{
String expected = "{\"id\":20200327,\"name\":\"SomeDopeName\",\"children\":[{\"id\":1,\"name\":\"children\"}],\"mapChildren\":{\"first\":{\"id\":0},\"second\":{\"id\":2,\"name\":\"children-name\"}}}";
String expected = "{\"id\":20200327,\"name\":\"SomeDope\\n\\nName\",\"children\":[{\"id\":1,\"name\":\"children\"}],\"mapChildren\":{\"first\":{\"id\":0},\"second\":{\"id\":2,\"name\":\"children-name\"}}}";
TestClass obj = new TestClass();
obj.id = 20200327l;
obj.name = "SomeDopeName";
obj.name = "SomeDope\n\nName";
obj.children = new ArrayList<>();
obj.children.add(new TestClass());
obj.children.get(0).id = 1l;
Expand Down

0 comments on commit 5ec2c8a

Please sign in to comment.