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

SunSpecCodeGenerator: refactor, add 7xx models, use JSON input files #2324

Merged
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
62 changes: 40 additions & 22 deletions io.openems.common/src/io/openems/common/utils/JsonUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,22 @@ public class JsonUtils {
/**
* Provide a easy way to generate a JsonArray from a list using the given
* convert function to add each element.
*
*
* @param list to convert
* @param convert function to convert elements
* @param <T> type of an element from list
*
*
* @return list as JsonArray
*/
public static <T> JsonArray generateJsonArray(Collection<T> list, Function<T, JsonElement> convert) {
if (list == null) {
return null;
} else {
var jab = new JsonArrayBuilder();
list.forEach(element -> {
jab.add(convert.apply(element));
});
return jab.build();
}
var jab = new JsonArrayBuilder();
list.forEach(element -> {
jab.add(convert.apply(element));
});
return jab.build();
}

/**
Expand Down Expand Up @@ -262,10 +261,10 @@ public JsonObjectBuilder addProperty(String property, Enum<?> value) {

/**
* Add a {@link ZonedDateTime} value to the {@link JsonObject}.
*
*
* <p>
* The value gets added in the format of {@link DateTimeFormatter#ISO_INSTANT}.
*
*
* @param property the key
* @param value the value
* @return the {@link JsonObjectBuilder}
Expand Down Expand Up @@ -364,10 +363,10 @@ public JsonObjectBuilder addPropertyIfNotNull(String property, Enum<?> value) {
/**
* Add a {@link ZonedDateTime} value to the {@link JsonObject} if it is not
* null.
*
*
* <p>
* The value gets added in the format of {@link DateTimeFormatter#ISO_INSTANT}.
*
*
* @param property the key
* @param value the value
* @return the {@link JsonObjectBuilder}
Expand Down Expand Up @@ -719,6 +718,19 @@ public static Optional<String> getAsOptionalString(JsonElement jElement, String
return Optional.ofNullable(toString(toPrimitive(toSubElement(jElement, memberName))));
}

/**
* Gets the member of the {@link JsonElement} as {@link String} if it exists,
* and the alternative value otherwise.
*
* @param jElement the {@link JsonElement}
* @param memberName the name of the member
* @param alternative the alternative value
* @return the {@link String} value or the alternative value
*/
public static String getAsStringOrElse(JsonElement jElement, String memberName, String alternative) {
return getAsOptionalString(jElement, memberName).orElse(alternative);
}

/**
* Converts a {@link JsonArray} to a String Array.
*
Expand Down Expand Up @@ -1373,17 +1385,20 @@ public static JsonElement getAsJsonElement(Object value) {
* String
*/
return new JsonPrimitive((String) value);
} else if (value instanceof Boolean) {
}
if (value instanceof Boolean) {
/*
* Boolean
*/
return new JsonPrimitive((Boolean) value);
} else if (value instanceof Inet4Address) {
}
if (value instanceof Inet4Address) {
/*
* Inet4Address
*/
return new JsonPrimitive(((Inet4Address) value).getHostAddress());
} else if (value instanceof JsonElement) {
}
if (value instanceof JsonElement) {
/*
* JsonElement
*/
Expand Down Expand Up @@ -1497,17 +1512,20 @@ public static Object getAsType(Class<?> type, JsonElement j) throws NotImplement
* Asking for an Long
*/
return j.getAsLong();
} else if (Boolean.class.isAssignableFrom(type)) {
}
if (Boolean.class.isAssignableFrom(type)) {
/*
* Asking for an Boolean
*/
return j.getAsBoolean();
} else if (Double.class.isAssignableFrom(type)) {
}
if (Double.class.isAssignableFrom(type)) {
/*
* Asking for an Double
*/
return j.getAsDouble();
} else if (String.class.isAssignableFrom(type)) {
}
if (String.class.isAssignableFrom(type)) {
/*
* Asking for a String
*/
Expand Down Expand Up @@ -1731,7 +1749,7 @@ public static boolean isEmptyJsonArray(JsonElement j) {

/**
* Check if the given {@link JsonElement} is a {@link Number}.
*
*
* @param j the {@link JsonElement} to check
* @return true if the element is a {@link Number}, otherwise false
*/
Expand All @@ -1742,7 +1760,7 @@ public static boolean isNumber(JsonElement j) {
/**
* Returns a sequential stream of the {@link JsonElement JsonElements} in the
* {@link JsonArray}.
*
*
* @param jsonArray The {@link JsonArray}, assumed to be unmodified during use
* @return a Stream of the elements
*/
Expand All @@ -1764,7 +1782,7 @@ private static JsonObject toJsonObject(JsonElement jElement) {
/**
* Returns a {@link Collector} that accumulates the input elements into a new
* {@link JsonObject}.
*
*
* @param <T> the type of the input
* @param keyMapper the key mapper
* @param valueMapper the value mapper
Expand All @@ -1785,7 +1803,7 @@ private static JsonObject toJsonObject(JsonElement jElement) {

/**
* Returns a Collector that accumulates the input elements into a new JsonArray.
*
*
* @return a Collector which collects all the input elements into a JsonArray
*/
public static Collector<JsonElement, JsonUtils.JsonArrayBuilder, JsonArray> toJsonArray() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,10 @@ public void testGetAsString() throws OpenemsNamedException {
// -> Optional Sub-Element
assertEquals("value", JsonUtils.getAsOptionalString(JSON_OBJECT, "String").get());
assertEquals(Optional.empty(), JsonUtils.getAsOptionalString(JSON_OBJECT, "foo"));

// -> As String or Else
assertEquals("value", JsonUtils.getAsStringOrElse(JSON_OBJECT, "String", "alternative"));
assertEquals("alternative", JsonUtils.getAsStringOrElse(JSON_OBJECT, "foo", "alternative"));
}

@Test
Expand Down
Loading