Skip to content
Open
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
bin/
target/
.history/

# IntelliJ IDEA files
.idea/
*.iml
6 changes: 3 additions & 3 deletions com.eclipsesource.json/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@
<configuration>
<signature>
<groupId>org.codehaus.mojo.signature</groupId>
<artifactId>java15</artifactId>
<artifactId>java18</artifactId>
<version>1.0</version>
</signature>
</configuration>
<executions>
<execution>
<id>ensure-java-1.5-class-library</id>
<id>ensure-java-1.8-class-library</id>
<phase>test</phase>
<goals>
<goal>check</goal>
Expand Down Expand Up @@ -82,7 +82,7 @@
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
<Bundle-Name>${project.name}</Bundle-Name>
<Bundle-Version>${project.version}</Bundle-Version>
<Bundle-RequiredExecutionEnvironment>J2SE-1.5</Bundle-RequiredExecutionEnvironment>
<Bundle-RequiredExecutionEnvironment>JavaSE-1.8</Bundle-RequiredExecutionEnvironment>
<Import-Package><!-- without this empty statement, a package import is added --></Import-Package>
<Export-Package>com.eclipsesource.json;version="${project.version}"</Export-Package>
</instructions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
import java.util.List;

import com.eclipsesource.json.JsonObject.Member;
import java.util.Optional;
import java.util.OptionalDouble;
import java.util.OptionalInt;
import java.util.OptionalLong;


/**
Expand Down Expand Up @@ -565,6 +569,8 @@ public JsonObject merge(JsonObject object) {
* the name of the member whose value is to be returned
* @return the value of the last member with the specified name, or <code>null</code> if this
* object does not contain a member with that name
* @see #getOptional(java.lang.String)
* equivalent using an Optional
*/
public JsonValue get(String name) {
if (name == null) {
Expand All @@ -573,6 +579,21 @@ public JsonValue get(String name) {
int index = indexOf(name);
return index != -1 ? values.get(index) : null;
}

/**
* Returns the value of the member with the specified name in this object. If this object contains
* multiple members with the given name, this method will return the last one.
*
* @param name
* the name of the member whose value is to be returned
* @return the value of the last member with the specified name, or an empty optional if this
* object does not contain a member with that name
* @see #get(java.lang.String)
* equivalent without Optional instantiation
*/
public Optional<JsonValue> getOptional(String name) {
return Optional.ofNullable(get(name));
}

/**
* Returns the <code>int</code> value of the member with the specified name in this object. If
Expand All @@ -587,11 +608,32 @@ public JsonValue get(String name) {
* the value to be returned if the requested member is missing
* @return the value of the last member with the specified name, or the given default value if
* this object does not contain a member with that name
* @see #getInt(java.lang.String)
* equivalent using an OptionalInt
*/
public int getInt(String name, int defaultValue) {
JsonValue value = get(name);
return value != null ? value.asInt() : defaultValue;
}

/**
* Returns the <code>int</code> value of the member with the specified name in this object. If
* this object does not contain a member with this name, an empty Optional is returned. If
* this object contains multiple members with the given name, the last one will be picked. If this
* member's value does not represent a JSON number or if it cannot be interpreted as Java
* <code>int</code>, an exception is thrown.
*
* @param name
* the name of the member whose value is to be returned
* @return the value of the last member with the specified name, or an empty Optional if
* this object does not contain a member with that name
* @see #getInt(java.lang.String, int)
* equivalent without Optional instantiation
*/
public OptionalInt getInt(String name) {
JsonValue value = get(name);
return value != null ? OptionalInt.of(value.asInt()) : OptionalInt.empty();
}

/**
* Returns the <code>long</code> value of the member with the specified name in this object. If
Expand All @@ -606,12 +648,33 @@ public int getInt(String name, int defaultValue) {
* the value to be returned if the requested member is missing
* @return the value of the last member with the specified name, or the given default value if
* this object does not contain a member with that name
* @see #getLong(java.lang.String)
* equivalent using an OptionalLong
*/
public long getLong(String name, long defaultValue) {
JsonValue value = get(name);
return value != null ? value.asLong() : defaultValue;
}

/**
* Returns the <code>long</code> value of the member with the specified name in this object. If
* this object does not contain a member with this name, an empty Optional is returned. If
* this object contains multiple members with the given name, the last one will be picked. If this
* member's value does not represent a JSON number or if it cannot be interpreted as Java
* <code>long</code>, an exception is thrown.
*
* @param name
* the name of the member whose value is to be returned
* @return the value of the last member with the specified name, or an empty Optional if
* this object does not contain a member with that name
* @see #getLong(java.lang.String, long)
* equivalent without Optional instantiation
*/
public OptionalLong getLong(String name) {
JsonValue value = get(name);
return value != null ? OptionalLong.of(value.asLong()) : OptionalLong.empty();
}

/**
* Returns the <code>float</code> value of the member with the specified name in this object. If
* this object does not contain a member with this name, the given default value is returned. If
Expand All @@ -625,11 +688,32 @@ public long getLong(String name, long defaultValue) {
* the value to be returned if the requested member is missing
* @return the value of the last member with the specified name, or the given default value if
* this object does not contain a member with that name
* @see #getFloat(java.lang.String)
* equivalent using an Optional
*/
public float getFloat(String name, float defaultValue) {
JsonValue value = get(name);
return value != null ? value.asFloat() : defaultValue;
}

/**
* Returns the <code>float</code> value of the member with the specified name in this object. If
* this object does not contain a member with this name, an empty Optional is returned. If
* this object contains multiple members with the given name, the last one will be picked. If this
* member's value does not represent a JSON number or if it cannot be interpreted as Java
* <code>float</code>, an exception is thrown.
*
* @param name
* the name of the member whose value is to be returned
* @return the value of the last member with the specified name, or an empty Optional if
* this object does not contain a member with that name
* @see #getFloat(java.lang.String, float)
* equivalent without Optional instantiation
*/
public Optional<Float> getFloat(String name) {
JsonValue value = get(name);
return value != null ? Optional.of(value.asFloat()) : Optional.<Float>empty();
}

/**
* Returns the <code>double</code> value of the member with the specified name in this object. If
Expand All @@ -644,11 +728,32 @@ public float getFloat(String name, float defaultValue) {
* the value to be returned if the requested member is missing
* @return the value of the last member with the specified name, or the given default value if
* this object does not contain a member with that name
* @see #getDouble(java.lang.String)
* equivalent using an OptionalDouble
*/
public double getDouble(String name, double defaultValue) {
JsonValue value = get(name);
return value != null ? value.asDouble() : defaultValue;
}

/**
* Returns the <code>double</code> value of the member with the specified name in this object. If
* this object does not contain a member with this name, an empty Optional is returned. If
* this object contains multiple members with the given name, the last one will be picked. If this
* member's value does not represent a JSON number or if it cannot be interpreted as Java
* <code>double</code>, an exception is thrown.
*
* @param name
* the name of the member whose value is to be returned
* @return the value of the last member with the specified name, or an empty Optional if
* this object does not contain a member with that name
* @see #getDouble(java.lang.String, double)
* equivalent without Optional instantiation
*/
public OptionalDouble getDouble(String name) {
JsonValue value = get(name);
return value != null ? OptionalDouble.of(value.asDouble()) : OptionalDouble.empty();
}

/**
* Returns the <code>boolean</code> value of the member with the specified name in this object. If
Expand All @@ -663,11 +768,32 @@ public double getDouble(String name, double defaultValue) {
* the value to be returned if the requested member is missing
* @return the value of the last member with the specified name, or the given default value if
* this object does not contain a member with that name
* @see #getBoolean(java.lang.String)
* equivalent using an Optional
*/
public boolean getBoolean(String name, boolean defaultValue) {
JsonValue value = get(name);
return value != null ? value.asBoolean() : defaultValue;
}

/**
* Returns the <code>boolean</code> value of the member with the specified name in this object. If
* this object does not contain a member with this name, an empty Optional is returned. If
* this object contains multiple members with the given name, the last one will be picked. If this
* member's value does not represent a JSON number or if it cannot be interpreted as Java
* <code>boolean</code>, an exception is thrown.
*
* @param name
* the name of the member whose value is to be returned
* @return the value of the last member with the specified name, or an empty Optional if
* this object does not contain a member with that name
* @see #getBoolean(java.lang.String, boolean)
* equivalent without Optional instantiation
*/
public Optional<Boolean> getBoolean(String name) {
JsonValue value = get(name);
return value != null ? Optional.of(value.asBoolean()) : Optional.<Boolean>empty();
}

/**
* Returns the <code>String</code> value of the member with the specified name in this object. If
Expand All @@ -681,12 +807,33 @@ public boolean getBoolean(String name, boolean defaultValue) {
* the value to be returned if the requested member is missing
* @return the value of the last member with the specified name, or the given default value if
* this object does not contain a member with that name
* @see #getString(java.lang.String)
* equivalent using an Optional
*/
public String getString(String name, String defaultValue) {
JsonValue value = get(name);
return value != null ? value.asString() : defaultValue;
}

/**
* Returns the <code>String</code> value of the member with the specified name in this object. If
* this object does not contain a member with this name, an empty Optional is returned. If
* this object contains multiple members with the given name, the last one will be picked. If this
* member's value does not represent a JSON number or if it cannot be interpreted as Java
* <code>String</code>, an exception is thrown.
*
* @param name
* the name of the member whose value is to be returned
* @return the value of the last member with the specified name, or an empty Optional if
* this object does not contain a member with that name
* @see #getString(java.lang.String, String)
* equivalent without Optional instantiation
*/
public Optional<String> getString(String name) {
JsonValue value = get(name);
return value != null ? Optional.of(value.asString()) : Optional.<String>empty();
}

/**
* Returns the number of members (name/value pairs) in this object.
*
Expand Down