Live data from Hacker News

JEP 540: Simple JSON API (Now in Incubator)

openjdk.org

81–83 of 83 posts

Re: JEP 540: Simple JSON API (Now in Incubator)

#81
post #77

A stated goal of the API is to have "low ceremony"; this seems like a lot of ceremony. IO.println(JsonObject.of(Map.of("providers", JsonArray.of(List.of(JsonString.of("SUN"), JsonString.of("SunRsaSign"), JsonString.of("SunEC")))))); There's gotta be a better way! Surely there could be some way of creating a JsonArray of native Java Strings, Booleans, Doubles, and Integers without requiring clients to explicitly conve…

I really wish the JDK devs would start using constructors again. I understand why factory methods exist but everything these days is of/from/newInstance/something else. Dart/Scala/Kotlin have all really solved this with language features and construction is uniform. For example in Dart they have factory constructors: https://dart.dev/language/constructors#factory-constructors

and for Kotlin/Scala you can use some cheeky operator overloading via companion objects.

    sealed Interface A {
        class B: A
        class C: A
        companion object {
            operator fun invoke(s: String) = when (s) {
                "B" -> B()
                "C" -> C()
            }
        }
    }

    val a = A("B")

Re: JEP 540: Simple JSON API (Now in Incubator)

#82

A stated goal of the API is to have "low ceremony"; this seems like a lot of ceremony. IO.println(JsonObject.of(Map.of("providers", JsonArray.of(List.of(JsonString.of("SUN"), JsonString.of("SunRsaSign"), JsonString.of("SunEC")))))); There's gotta be a better way! Surely there could be some way of creating a JsonArray of native Java Strings, Booleans, Doubles, and Integers without requiring clients to explicitly conve…

There's a nice Java library called Clojure with a lightweight syntax if you need to work with data structures and concurrency in Java a lot: (println (json/generate-string {:providers ["SUN" "SunRsaSign" "SunEC"]}))

Ironically as a Clojure developer I would really love a JDK native JSON library that we could wrap to avoid having to ship 3rd party JSON deps. Sort of like how java.net.http made things simpler.

Re: JEP 540: Simple JSON API (Now in Incubator)

#83
post #79
post #51

Earlier quoted context omitted.

> there's really not that much scope to creep into First, we've been in this game far too long to know that this isn't the case. Second, this is only incubation. It may well be that the team behind this feature intend to add more convenience methods but wish to do it later once the core is more battle-tested. It's always best to focus on the core first and add ornamentation once you know the core is right.

Shouldn't the core provided by this JEP be a streaming API then, so that you can build whatever you need on top of it? But they specifically exclude that from the goals.

No, because the functionality this is the core of is explicitly for small, simple JSON tasks. If the core were streaming, you would need to always write quite a bit of non-trivial code for small, simple JSON tasks. Note that the lack of some convenience methods here doesn't even increase the number of lines of code needed. It just makes the lines more noisy.
Post reply on HN