/jsonviewer-compose

Android JSON Viewer (Jetpack Compose)

Primary LanguageKotlin

Android JSON Viewer

"Buy Me A Coffee"

Generic badge


JSON Viewer and Beautifier

A lightweight package that will read JSON data, expand and collapse JSON view accordingly.


preview.mp4

Easiest way to format Json String

JsonViewerAdapter(jsonElement = {PUT_YOUR_JSON_ELEMENT})

Table of Contents

Add it in your root build.gradle at the end of repositories:

allprojects {
  repositories {
    // ...
    maven { url 'https://jitpack.io' }
  }
}

Add the dependency

implementation 'com.github.pokeum:jsonviewer-compose:0.0.0'
  • JsonParser.parse()

    Convert String into a JsonElement object

    Example - Parsing JSON

    val jsonString = "{ \"abc\": \"def\",\"xyz\": 123 }"
              
    val jsonParser = JsonParser.Builder().build()
    val jsonElement: JsonElement? = try {
      jsonParser.parse(jsonString)
    }
    // Raise a JSONException if it is not a JSONObject or JSONArray.
    catch (e: JSONException) { null }

    Display JSON

    JsonViewerAdapter(jsonElement = {PUT_YOUR_JSON_ELEMENT})
  • JsonParser - Sort JSON

    Example - Alphabetically

    JsonParser.Builder()
      .setComparator(compareBy { it.key })
      .build()

Color

Key "friends", "0", "name", "age"
Value "Alice", 28
Splitter :
Type ABC, 123
Arrow
Bracket [ ], { }
Divider
JsonViewerAdapter(
  // ...
  keyColor = JsonViewerColor(colorResource(R.color.key_color)),
  valueColor = JsonViewerColor(colorResource(R.color.value_color)),
  splitterColor = JsonViewerColor(colorResource(R.color.splitter_color)),
  typeColor = JsonViewerColor(colorResource(R.color.type_color)),
  arrowColor = JsonViewerColor(colorResource(R.color.arrow_color)),
  bracketColor = JsonViewerColor(colorResource(R.color.bracket_color)),
  dividerColor = JsonViewerColor(colorResource(R.color.divider_color))
)