pojson-native-json

Kotlin library for json formatting bringing pretty DSL

License

License

License
Categories

Categories

JSON Data Native Development Tools
GroupId

GroupId

io.github.acelost
ArtifactId

ArtifactId

pojson-native-json
Last Version

Last Version

0.0.5
Release Date

Release Date

Type

Type

aar
Description

Description

pojson-native-json
Kotlin library for json formatting bringing pretty DSL
Project URL

Project URL

https://github.com/acelost/Pojson
Source Code Management

Source Code Management

https://github.com/acelost/Pojson/tree/main

Download pojson-native-json

How to add to project

<!-- https://jarcasting.com/artifacts/io.github.acelost/pojson-native-json/ -->
<dependency>
    <groupId>io.github.acelost</groupId>
    <artifactId>pojson-native-json</artifactId>
    <version>0.0.5</version>
    <type>aar</type>
</dependency>
// https://jarcasting.com/artifacts/io.github.acelost/pojson-native-json/
implementation 'io.github.acelost:pojson-native-json:0.0.5'
// https://jarcasting.com/artifacts/io.github.acelost/pojson-native-json/
implementation ("io.github.acelost:pojson-native-json:0.0.5")
'io.github.acelost:pojson-native-json:aar:0.0.5'
<dependency org="io.github.acelost" name="pojson-native-json" rev="0.0.5">
  <artifact name="pojson-native-json" type="aar" />
</dependency>
@Grapes(
@Grab(group='io.github.acelost', module='pojson-native-json', version='0.0.5')
)
libraryDependencies += "io.github.acelost" % "pojson-native-json" % "0.0.5"
[io.github.acelost/pojson-native-json "0.0.5"]

Dependencies

compile (2)

Group / Artifact Type Version
io.github.acelost : pojson-core jar 0.0.5
org.jetbrains.kotlin : kotlin-stdlib jar 1.4.32

Project Modules

There are no modules declared in this project.

Pojson

Pojson is a kotlin library for json prototyping. It brings DSL for building JsonObjectPrototype and JsonArrayPrototype. Prototypes don't reference to any json object/array models. You can choose implementation you want to use for rendering json. Simple json prototype may looks like:

val prototype = JsonObjectPrototype {
    "my-string-property" % "Hello, world!"
    "my-number-property" % 42L
    "my-object-property" % obj {
        "my-nested-object-property" % true
    }
    "my-array-property" % array {
        element("Hello, world!")
        element(42L)
        element(true)
    }
}

Usage

Actually Pojson is a kotlin class with methods render for object and array.

val prototype = JsonObjectPrototype {
    ..
}
val pojson = Pojson(..)
pojson.render(prototype) // Returns selected json object representation

Pojson constructor requires implementation of interfaces JsonObjectFactory, JsonArrayFactory, JsonObjectAdapter, JsonArrayAdapter. Usually implementation of this interfaces is a simple task. You can implement this interfaces manually or use one of existing integration with json object/array representation.

val prototype = JsonObjectPrototype {
    ..
}
Pojson2Gson.create().render(prototype) // Returns com.google.gson.JsonObject
Pojson2NativeJSON.create().render(prototype) // Returns org.json.JSONObject
Pojson2NativeCollection.create().render(prototype) // Returns java.util.Map

Syntax

Create object

Pojson provides class JsonObjectPrototype with constructor as entry point for object notation.

JsonObjectPrototype {
    ..
}

Assign property

Pojson provides overloaded operator % for assigning value to key in object notation.

JsonObjectPrototype {
    "my-property" % "my-value"
}

Create array

Pojson provides class JsonArrayPrototype with constructor as entry point for array notation.

JsonArrayPrototype {
    ..
}

Append element

Pojson provides element(..) method for appending items to array.

JsonArrayPrototype {
    element(42L)
    element("Hello, world!")
}

Nested object

Pojson provides method obj { .. } for nested object instantiation.

JsonObjectPrototype {
    "my-property" % obj {
        "my-nested-property" % "my-nested-value"
    }
}

Nested array

Pojson provides method array { .. } for nested array instantiation.

JsonObjectPrototype {
    "my-property" % array {
        ..
    }
}

Nullability

By default pojson requires all values to be non-null. If you want to work with nullable values you should specify it explicitly. Pojson provides method nullable for this purpose.

val myString: String? = "Hello, world!"
// Not working
JsonObjectPrototype {
    "my-property" % myString // IDE error: expected [String] but found [String?]
}
// Working
JsonObjectPrototype {
    "my-property" % nullable(myString) // No error, correct type inference
}

Also pojson provides utility methods nullString, nullNumber, nullBoolean, nullObject and nullArray
for kotlin type inference when you want to use if-else expressions inside object/array notation.

JsonObjectPrototype {
    "my-property" % if (myFlag) obj {
        ..
    } else nullObject()
}

Composition

Pojson provides capability to composite prototypes.

val manufacturer = JsonObjectPrototype {
    "name" % "Land Rover"
    "country" % "United Kingdom"
}
val car = JsonObjectPrototype {
    "model" % "Defender"
    "year" % 2017
    "manufacturer" % manufacturer
}

Merge

Pojson provides capability to merge prototype properties into another prototype.

val design = JsonObjectPrototype {
    "color" % "black"
    "wheels" % "20.0 inch"
}
val car = JsonObjectPrototype {
    "model" % "Defender"
    "year" % 2017
    merge(design)
}

Integration

Core

Maven Central

Core module contains syntax declaration without integrations with third-party solutions.

dependencies {
    implementation 'io.github.acelost:pojson-core:${latestVersion}'
}

Gson

Maven Central

Gson module contains syntax declaration plus integration with gson object model. You can render prototypes to com.google.gson.JsonObject and com.google.gson.JsonArray.

dependencies {
    implementation 'io.github.acelost:pojson-gson:${latestVersion}'
}

Native Json

Maven Central

Native Json module contains syntax declaration plus integration with native android object model. You can render prototypes to org.json.JSONObject and org.json.JSONArray.

dependencies {
    implementation 'io.github.acelost:pojson-native-json:${latestVersion}'
}

Native Collection

Maven Central

Native Collection module contains syntax declaration plus integration with native java collections. You can render prototypes to java.util.Map and java.util.List.

dependencies {
    implementation 'io.github.acelost:pojson-native-collection:${latestVersion}'
}

Versions

Version
0.0.5
0.0.4
0.0.3