Pojson provides Kotlin DSL for building complex jsons in declarative manner.

Related tags

JSON Pojson
Overview

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}'
}
You might also like...
GmailCompose is an Android application 📱 for showcasing Jetpack Compose for building declarative UI in Android.
GmailCompose is an Android application 📱 for showcasing Jetpack Compose for building declarative UI in Android.

GmailCompose GmailCompose Demo GmailCompose is an Android application 📱 for showcasing Jetpack Compose for building declarative UI in Android. About

Cago provides you way to do complex calculations on your device.

Cago Do your calculations easier. Cago provides you way to do complex calculations on your device. You can build functions that fit your goals by your

Kotter - aims to be a relatively thin, declarative, Kotlin-idiomatic API that provides useful functionality for writing delightful console applications.
Kotter - aims to be a relatively thin, declarative, Kotlin-idiomatic API that provides useful functionality for writing delightful console applications.

Kotter (a KOTlin TERminal library) aims to be a relatively thin, declarative, Kotlin-idiomatic API that provides useful functionality for writing delightful console applications.

This project provides declarative camunda delegates for Spring based application

camunda-delegator-lib Features Declarative style for delegate code Generated delegates documentation and templates for camunda modeler(this feature is

Kotlin-client-dsl - A kotlin-based dsl project for a (Client) - (Plugin) styled program

kotlin-client-dsl a kotlin-based dsl project for a (Client) - (Plugin) styled p

A lightweight Kotlin DSL to render DSL style Json to String.
A lightweight Kotlin DSL to render DSL style Json to String.

A lightweight Kotlin DSL to render DSL style Json to String.

A small DSL to make building a conversation in Bukkit easy.

Konversation Konversation provides a simple builder to construct chains of prompts to be used in the conversation API present in Bukkit. Bukkit only p

UW Homework: Complex Kotlin

UW Homework: Complex Kotlin This homework is designed to force you to exercise y

Android library to create complex multi-state animations.
Android library to create complex multi-state animations.

MultiStateAnimation Android library to create complex multi-state animations. Overview A class that allows for complex multi-state animations using An

Easily create complex recyclerview adapters in android
Easily create complex recyclerview adapters in android

😔 Due to the nature of my job and growing popularity of Jetpack Compose, I lack the motivation to keep this project alive. Recyclerview is one of the

Tool for create complex morphing animations using VectorDrawables (allows morphing between any pair of SVG images)

VectAlign VectAlign (a.k.a. VectorDrawableAlign) is a developer's tool which automagically aligns two VectorDrawable "pathData" strings (or SVG images

FixedHeaderTableLayout is a powerful Android library for displaying complex data structures and rendering tabular data composed of rows, columns and cells with scrolling and zooming features. FixedHeaderTableLayout is similar in construction and use as to Android's TableLayout
FixedHeaderTableLayout is a powerful Android library for displaying complex data structures and rendering tabular data composed of rows, columns and cells with scrolling and zooming features. FixedHeaderTableLayout is similar in construction and use as to Android's TableLayout

FixedHeaderTableLayout is a powerful Android library for displaying complex data structures and rendering tabular data composed of rows, columns and cells with scrolling and zooming features. FixedHeaderTableLayout is similar in construction and use as to Android's TableLayout

a set of Settings like composable items to help android Jetpack Compose developers build complex settings screens
a set of Settings like composable items to help android Jetpack Compose developers build complex settings screens

This library provides a set of Settings like composable items to help android Jetpack Compose developers build complex settings screens without all the boilerplate

A complex of libraries and starters for organizing long-polling-based interaction between the client and the server.

A complex of libraries and starters for organizing long-polling-based interaction between the client and the server.

The Spigot plugin counterpart of the overly complex SSN.gg authentication system

Atreus The Spigot plugin counterpart of the overly complex SSN.gg authentication system. Building Make sure you have both Maven and JDK installed (ver

Provides Kotlin libs and some features for building Kotlin plugins
Provides Kotlin libs and some features for building Kotlin plugins

Kotlin Plugin Provides Kotlin libs and some features for building awesome Kotlin plugins. Can be used instead of CreeperFace's KotlinLib (don't use to

Provides Ktor Server libs for building awesome Kotlin plugins which needs to provide builtin HTTP servers
Provides Ktor Server libs for building awesome Kotlin plugins which needs to provide builtin HTTP servers

Ktor Plugin Provides Ktor Server libs for building awesome Kotlin plugins which needs to provide builtin HTTP servers. Requires: https://github.com/Po

A declarative, Kotlin-idiomatic API for writing dynamic command line applications.
A declarative, Kotlin-idiomatic API for writing dynamic command line applications.

A declarative, Kotlin-idiomatic API for writing dynamic command line applications.

Kotlin Multiplatform Mobile + Mobile Declarative UI Framework (Jetpack Compose and SwiftUI)

Kotlin Multiplatform Mobile + Mobile Declarative UI Framework (Jetpack Compose and SwiftUI)

Releases(0.6)
Owner
Alexander Mironychev
Alexander Mironychev
A modern JSON library for Kotlin and Java.

Moshi Moshi is a modern JSON library for Android and Java. It makes it easy to parse JSON into Java objects: String json = ...; Moshi moshi = new Mos

Square 8.7k Jan 2, 2023
Kotlin extensions for Moshi, Make every thing you want with Moshi in just one line.

Kotlin extensions for Moshi, Make every thing with square / Moshi in one line.

Mazen Rashed 15 Nov 21, 2021
Modern JSON processor with readable Kotlin syntax.

Kq Modern cross-platform JSON processor with readable Kotlin syntax. cat ~/Desktop/bdb.ndjson | kq '.filter{it.bool("muted")}.sortedBy{it.long("size")

Daniel Demidko 6 Jan 25, 2022
Manager of progress using Lottie JSON, compatible for Java and Kotlin.

Progress Lottie IGB Manager of progress using Lottie JSON, compatible for Java and Kotlin. Int Top In Bottom Important Info: To create a raw folder: R

Isaac G. Banda 21 Sep 16, 2022
Customizable JSON Schema-based forms for Kotlin and Compose

Kotlin JSON Forms Customizable JSON Schema-based forms for Kotlin and Compose This project aims to reimplement JSON Forms in Kotlin for use in Compose

Copper Leaf 3 Oct 1, 2022
HorizontalListView is an Android ListView widget which scrolls in a horizontal manner (in contrast with the SDK-provided ListView which scrolls vertically).

HorizontalListView HorizontalListView is an Android ListView widget which scrolls in a horizontal manner (in contrast with the SDK-provided ListView w

MeetMe 722 Nov 10, 2022
Epoxy is an Android library for building complex screens in a RecyclerView

Epoxy Epoxy is an Android library for building complex screens in a RecyclerView. Models are automatically generated from custom views or databinding

Airbnb 8.1k Jan 4, 2023
Epoxy is an Android library for building complex screens in a RecyclerView

Epoxy Epoxy is an Android library for building complex screens in a RecyclerView. Models are automatically generated from custom views or databinding

Airbnb 8.1k Dec 29, 2022
{ } Declarative Kotlin DSL for choreographing Android transitions

Transition X Kotlin DSL for choreographing Android Transitions TransitionManager makes it easy to animate simple changes to layout without needing to

Arunkumar 520 Dec 16, 2022
A declarative framework for building efficient UIs on Android.

Litho Litho is a declarative framework for building efficient UIs on Android. Declarative: Litho uses a declarative API to define UI components. You s

Facebook 7.5k Jan 4, 2023