Skip to content

Improvement for JSONObject class #295

Closed

Description

@kpopovic

Hello,
please add new methods to JSONObject class. I think the can be very useful.

byte[] getBinary(String key) / byte[] getBinary(String key, byte[] def)

JSON itself has no notion of a binary, this extension complies to the RFC-7493, so this method assumes there is a String value with the key and it contains a Base64 encoded binary, which it decodes if found and returns.

public JsonObject put(String key, byte[] value)

Put a byte[] into the JSON object with the specified key.
JSON extension RFC7493, binary will first be Base64 encoded before being put as a String.

public java.time.Instant getInstant(String key)

Get the instant value with the specified key.
JSON itself has no notion of a date, this extension complies to the RFC-7493, so this method assumes there is a String value with the key and it contains a ISODATE encoded date, which it decodes if found and returns.

example:
public Instant getInstant(String key) {
Objects.requireNonNull(key);
String encoded = (String) map.get(key);
return encoded == null ? null : Instant.from(ISO_INSTANT.parse(encoded));
}

java.time.Instant getInstant(String key, java.time.Instant def)

Like getInstant(String) but specifying a default value to return if there is no entry.

example:
public Instant getInstant(String key, Instant def) {
Objects.requireNonNull(key);
Object val = map.get(key);
return val != null || map.containsKey(key) ?
(val == null ? null : Instant.from(ISO_INSTANT.parse((String) val))) : def;
}

public JsonObject put(String key, java.time.Instant value)

Put a Instant into the JSON object with the specified key.
JSON extension RFC7493, instant will first be encoded to ISODATE String.

example:
import static java.time.format.DateTimeFormatter.ISO_INSTANT;
.....
public JsonObject put(String key, Instant value) {
Objects.requireNonNull(key);
map.put(key, value == null ? null : ISO_INSTANT.format(value));
return this;
}

public JsonObject copy()
........................................
Copy the JSON object

Activity

  1. johnjaylward commented on Oct 19, 2016

    @johnjaylward
    Contributor

    We don't currently support java8, so I don't see the Instant items being
    implemented.

    On Oct 19, 2016 18:03, "Krešimir Popović" [email protected] wrote:

    Hello,
    please add new methods to JSONObject class. I think the can be very useful.
    byte[] getBinary(String key) / byte[] getBinary(String key, byte[] def)

    JSON itself has no notion of a binary, this extension complies to the
    RFC-7493, so this method assumes there is a String value with the key and
    it contains a Base64 encoded binary, which it decodes if found and returns.
    public JsonObject put(String key, byte[] value)

    Put a byte[] into the JSON object with the specified key.
    JSON extension RFC7493, binary will first be Base64 encoded before being
    put as a String.
    public java.time.Instant getInstant(String key)

    Get the instant value with the specified key.
    JSON itself has no notion of a date, this extension complies to the
    RFC-7493, so this method assumes there is a String value with the key and
    it contains a ISODATE encoded date, which it decodes if found and returns.

    example:
    public Instant getInstant(String key) {
    Objects.requireNonNull(key);
    String encoded = (String) map.get(key);
    return encoded == null ? null : Instant.from(ISO_INSTANT.parse(encoded));
    }

    java.time.Instant getInstant(String key, java.time.Instant def)
    Like getInstant(String) but specifying a default value to return if there
    is no entry.

    example:
    public Instant getInstant(String key, Instant def) {
    Objects.requireNonNull(key);
    Object val = map.get(key);
    return val != null || map.containsKey(key) ?
    (val == null ? null : Instant.from(ISO_INSTANT.parse((String) val))) :
    def;
    }
    public JsonObject put(String key, java.time.Instant value)

    Put a Instant into the JSON object with the specified key.
    JSON extension RFC7493, instant will first be encoded to ISODATE String.

    example:
    import static java.time.format.DateTimeFormatter.ISO_INSTANT;
    .....
    public JsonObject put(String key, Instant value) {
    Objects.requireNonNull(key);
    map.put(key, value == null ? null : ISO_INSTANT.format(value));
    return this;
    }

    public JsonObject copy()
    ........................................
    Copy the JSON object

    —
    You are receiving this because you are subscribed to this thread.
    Reply to this email directly, view it on GitHub
    #295, or mute the thread
    https://github.com/notifications/unsubscribe-auth/AAXa18YBKN4_fig1hlWozCJmYCfn1KQoks5q1pO4gaJpZM4KbgcW
    .

  2. kpopovic commented on Oct 19, 2016

    @kpopovic
    Author

    Java 8 was released on 18 March 2014, more then 2 years ago...
    Do you plan to support Java 8 at all ?

  3. johnjaylward commented on Oct 19, 2016

    @johnjaylward
    Contributor

    We are currently supporting java6 and up, but that means no java8 specific
    features

    On Oct 19, 2016 6:17 PM, "Krešimir Popović" [email protected]
    wrote:

    Java 8 was released on 18 March 2014, more then 2 years ago...
    Do you plan to support Java 8 at all ?

    —
    You are receiving this because you commented.
    Reply to this email directly, view it on GitHub
    #295 (comment),
    or mute the thread
    https://github.com/notifications/unsubscribe-auth/AAXa14OuCqXGY-B5_4pPKrD61Qob8qv-ks5q1pbngaJpZM4KbgcW
    .

  4. kpopovic commented on Oct 19, 2016

    @kpopovic
    Author

    Ok, I understand.

  5. stleary commented on Oct 20, 2016

    @stleary
    Owner

    Not adding support for date or time transforms at this time. No objection if someone sees a need for Base64 and wants to try implementing it in a way that does not impact existing applications.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions