Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure the node type is set when loading empty lists and objects #120

Closed
wants to merge 1 commit into from
Closed
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
Expand Up @@ -17,6 +17,8 @@
package ninja.leaping.configurate.gson;

import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.gson.JsonParseException;
import com.google.gson.stream.JsonReader;
Expand Down Expand Up @@ -170,14 +172,21 @@ private void parseValue(JsonReader parser, ConfigurationNode node) throws IOExce

private void parseArray(JsonReader parser, ConfigurationNode node) throws IOException {
parser.beginArray();

boolean written = false;
JsonToken token;
while ((token = parser.peek()) != null) {
switch (token) {
case END_ARRAY:
parser.endArray();
// ensure the type is preserved
if (!written) {
node.setValue(ImmutableList.of());
}
return;
default:
parseValue(parser, node.getAppendedNode());
written = true;
}
}
throw new JsonParseException("Reached end of stream with unclosed array at!");
Expand All @@ -186,15 +195,22 @@ private void parseArray(JsonReader parser, ConfigurationNode node) throws IOExce

private void parseObject(JsonReader parser, ConfigurationNode node) throws IOException {
parser.beginObject();

boolean written = false;
JsonToken token;
while ((token = parser.peek()) != null) {
switch (token) {
case END_OBJECT:
case END_DOCUMENT:
parser.endObject();
// ensure the type is preserved
if (!written) {
node.setValue(ImmutableMap.of());
}
return;
case NAME:
parseValue(parser, node.getNode(parser.nextName()));
written = true;
break;
default:
throw new JsonParseException("Received improper object value " + token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package ninja.leaping.configurate.hocon;

import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.typesafe.config.Config;
Expand Down Expand Up @@ -184,18 +185,23 @@ private static void readConfigValue(ConfigValue value, CommentedConfigurationNod
}
switch (value.valueType()) {
case OBJECT:
if (((ConfigObject) value).isEmpty()) {
ConfigObject object = (ConfigObject) value;
if (object.isEmpty()) {
node.setValue(ImmutableMap.of());
} else {
for (Map.Entry<String, ConfigValue> ent : ((ConfigObject) value).entrySet()) {
for (Map.Entry<String, ConfigValue> ent : object.entrySet()) {
readConfigValue(ent.getValue(), node.getNode(ent.getKey()));
}
}
break;
case LIST:
List<ConfigValue> values = (ConfigList) value;
for (int i = 0; i < values.size(); ++i) {
readConfigValue(values.get(i), node.getNode(i));
ConfigList list = (ConfigList) value;
if (list.isEmpty()) {
node.setValue(ImmutableList.of());
} else {
for (int i = 0; i < list.size(); ++i) {
readConfigValue(list.get(i), node.getNode(i));
}
}
break;
case NULL:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import ninja.leaping.configurate.ConfigurationNode;
import ninja.leaping.configurate.ConfigurationOptions;
Expand Down Expand Up @@ -191,26 +193,38 @@ private static void parseValue(JsonParser parser, ConfigurationNode node) throws
}

private static void parseArray(JsonParser parser, ConfigurationNode node) throws IOException {
boolean written = false;
JsonToken token;
while ((token = parser.nextToken()) != null) {
switch (token) {
case END_ARRAY:
// ensure the type is preserved
if (!written) {
node.setValue(ImmutableList.of());
}
return;
default:
parseValue(parser, node.getAppendedNode());
written = true;
}
}
throw new JsonParseException(parser, "Reached end of stream with unclosed array!", parser.getCurrentLocation());
}

private static void parseObject(JsonParser parser, ConfigurationNode node) throws IOException {
boolean written = false;
JsonToken token;
while ((token = parser.nextToken()) != null) {
switch (token) {
case END_OBJECT:
// ensure the type is preserved
if (!written) {
node.setValue(ImmutableMap.of());
}
return;
default:
parseValue(parser, node.getNode(parser.getCurrentName()));
written = true;
}
}
throw new JsonParseException(parser, "Reached end of stream with unclosed array!", parser.getCurrentLocation());
Expand Down