Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
184 changes: 99 additions & 85 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[!["Buy Me A Coffee"](https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png)](https://www.buymeacoffee.com/pokeumcho)

[![Generic badge](https://img.shields.io/badge/jitpack-v0.0.2-darkyellow?logo=jitpack&logoColor=white.svg)](https://jitpack.io/#pokeum/jsonviewer-xml/)
[![Generic badge](https://img.shields.io/badge/jitpack-v1.0.0-darkyellow?logo=jitpack&logoColor=white.svg)](https://jitpack.io/#pokeum/jsonviewer-xml/)

---

Expand All @@ -24,24 +24,16 @@ https://github.com/pokeum/jsonviewer-xml/assets/102505472/e2f260f0-cc28-4607-9ec

---

**Easiest way to format Json String**
### Easiest way to format Json String

```xml
<kr.pokeum.jsonviewer_xml.JsonRecyclerView
<co.pokeum.jsonviewer.xml.JsonRecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:text="{PUT_YOUR_JSON_STRING}" />
```

## Table of Contents
- [Installation](#installation)
- [Usage](#usage)
- [Basic](#basic)
- [Advance](#advance)
- [Custom Styles](#styles)


## <a id="installation"> Installation
## Installation

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

Expand All @@ -57,103 +49,125 @@ allprojects {
Add the dependency

```gradle
implementation 'com.github.pokeum:jsonviewer-xml:0.0.2'
implementation 'com.github.pokeum:jsonviewer-xml:1.0.0'
```

## <a id="usage"> Usage
## Usage

- ## <a id="basic"> Basic
### JsonParser.parse()

### JsonParser.parse()

Convert `String` into a `JsonElement` object
Convert `String` into a `JsonElement` object

**Example - Parsing JSON**
```kotlin
val jsonString = "{ \"abc\": \"def\",\"xyz\": 123 }"
**Example - Parsing JSON**

```kotlin
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 }
```
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
### Use JsonRecyclerView to display JSON

#### [Method 1] Use JsonRecyclerView
Add `JsonRecyclerView` in XML layout file:

Add `JsonRecyclerView` in XML layout file:
```xml
<kr.pokeum.jsonviewer_xml.JsonRecyclerView
android:id="@+id/jsonViewer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:text="{PUT_YOUR_JSON_STRING}" />
```
```xml
<co.pokeum.jsonviewer.xml.JsonRecyclerView
android:id="@+id/jsonViewer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:text="{PUT_YOUR_JSON_STRING}" />
```

Change `JsonRecyclerView` text from the code:
```kotlin
findViewById<JsonRecyclerView>(R.id.jsonViewer).setText("{PUT_YOUR_JSON_STRING}")
```
Change `JsonRecyclerView` text from the code:

#### [Method 2] Use RecyclerView
```kotlin
findViewById<JsonRecyclerView>(R.id.jsonViewer).setText("{PUT_YOUR_JSON_STRING}")
```

Add `RecyclerView` in XML layout file:
```xml
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/jsonViewer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:ignore="SpeakableTextPresentCheck"
tools:listitem="@layout/item_json_object" />
```
### Use RecyclerView to display JSON

Add `RecyclerView` in XML layout file:

```xml
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/jsonViewer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:ignore="SpeakableTextPresentCheck"
tools:listitem="@layout/item_json_object" />
```

Set Adapter in `RecyclerView`:
```kotlin
val recyclerView = findViewById<RecyclerView>(R.id.jsonViewer)
recyclerView.adapter = JsonViewerAdapter(/* JsonElement */)
```
Set Adapter in `RecyclerView`:

- ## <a id="advance"> Advance
```kotlin
val recyclerView = findViewById<RecyclerView>(R.id.jsonViewer)
recyclerView.adapter = JsonViewerAdapter(/* JsonElement */)
```

### JsonParser - Sort JSON
## Advance

**Example - Alphabetically**
```kotlin
JsonParser.Builder()
.setComparator(compareBy { it.key })
.build()
```
### JsonParser - Sort JSON

**Example - Alphabetically**

### JsonElement - Save and Restore
```kotlin
JsonParser.Builder()
.setComparator(compareBy { it.key })
.build()
```

Save and Restore Data on Configuration Changed in Android using Bundle
### JsonElement - Save and Restore

Save and Restore Data on Configuration Changed in Android using Bundle

```kotlin
class YourActivity : AppCompatActivity() {
```kotlin
class YourActivity : AppCompatActivity() {

private var jsonElement: JsonElement? = null
private var jsonElement: JsonElement? = null

override fun onCreate(savedInstanceState: Bundle?) {
// ...
override fun onCreate(savedInstanceState: Bundle?) {
// ...

if (savedInstanceState != null) {
jsonElement = savedInstanceState.getParcelable("JSON_ELEMENT_KEY") /* Restore */
}
if (savedInstanceState != null) {
jsonElement = savedInstanceState.getParcelable("JSON_ELEMENT_KEY") /* Restore */
}
}

override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putParcelable("JSON_ELEMENT_KEY", jsonElement) /* Save */
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putParcelable("JSON_ELEMENT_KEY", jsonElement) /* Save */
}
```
}
```

## <a id="styles"> Custom Styles
### Expand All & Collapse All

#### Use JsonRecyclerView

```kotlin
findViewById<JsonRecyclerView>(R.id.jsonViewer).expandAll()
findViewById<JsonRecyclerView>(R.id.jsonViewer).collapseAll()
```

#### Use RecyclerView

```kotlin
val recyclerView = findViewById<RecyclerView>(R.id.jsonViewer)
recyclerView.adapter = JsonViewerAdapter(/* JsonElement */)

val jsonViewerAdapter = recyclerView.adapter as JsonViewerAdapter
jsonViewerAdapter.expandAll()
jsonViewerAdapter.collapseAll()
```

## Custom Styles

### Color

Expand All @@ -170,7 +184,7 @@ implementation 'com.github.pokeum:jsonviewer-xml:0.0.2'
#### Use JsonRecyclerView

```xml
<kr.pokeum.jsonviewer_xml.JsonRecyclerView
<co.pokeum.jsonviewer.xml.JsonRecyclerView
...
app:keyColor="@color/key_color"
app:valueColor="@color/value_color"
Expand All @@ -193,4 +207,4 @@ implementation 'com.github.pokeum:jsonviewer-xml:0.0.2'
setBracketColor(JsonViewerColor(/* ... */))
setDividerColor(JsonViewerColor(/* ... */))
}
```
```
10 changes: 2 additions & 8 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ android {
buildToolsVersion BUILD_TOOL_VERSION

defaultConfig {
applicationId "kr.pokeum.jsonviewer.app"
applicationId "co.pokeum.jsonviewer.app"
minSdkVersion MIN_SDK_VERSION
targetSdkVersion TARGET_SDK_VERSION
versionCode VERSION_CODE
Expand Down Expand Up @@ -55,11 +55,8 @@ android {
}

dependencies {

implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar'])

// implementation(project(":jsonviewer"))
implementation 'com.github.pokeum:jsonviewer-xml:0.0.2'
implementation(project(":jsonviewer"))

implementation stdlib.kotlin
implementation androidx.core
Expand All @@ -73,7 +70,4 @@ dependencies {
implementation platform(firebase.bom)

testImplementation test.junit

androidTestImplementation androidTest.junit
androidTestImplementation androidTest.espressoCore
}
6 changes: 3 additions & 3 deletions app/google-services.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
"project_info": {
"project_number": "1019350039203",
"project_id": "jsonviewer-86f73",
"storage_bucket": "jsonviewer-86f73.appspot.com"
"storage_bucket": "jsonviewer-86f73.firebasestorage.app"
},
"client": [
{
"client_info": {
"mobilesdk_app_id": "1:1019350039203:android:9cd8b741a970ae86e2dd16",
"mobilesdk_app_id": "1:1019350039203:android:54eb2638c6d6c410e2dd16",
"android_client_info": {
"package_name": "kr.pokeum.jsonviewer.app"
"package_name": "co.pokeum.jsonviewer.app"
}
},
"oauth_client": [],
Expand Down
24 changes: 0 additions & 24 deletions app/src/androidTest/java/kr/pokeum/app/ExampleInstrumentedTest.kt

This file was deleted.

2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="kr.pokeum.app">
package="co.pokeum.app">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package kr.pokeum.app
package co.pokeum.app

import android.content.Context
import android.net.Uri
import android.widget.Toast
import kr.pokeum.app.util.readAssetsFile
import kr.pokeum.app.util.readCacheFile
import kr.pokeum.app.util.readFileFromUri
import kr.pokeum.app.util.writeCacheFile
import kr.pokeum.jsonviewer_xml.JsonParser
import kr.pokeum.jsonviewer_xml.model.JsonElement
import kr.pokeum.jsonviewer_xml.model.JsonObject
import co.pokeum.app.util.readAssetsFile
import co.pokeum.app.util.readCacheFile
import co.pokeum.app.util.readFileFromUri
import co.pokeum.app.util.writeCacheFile
import co.pokeum.jsonviewer.xml.JsonParser
import co.pokeum.jsonviewer.xml.model.JsonElement
import co.pokeum.jsonviewer.xml.model.JsonObject
import org.json.JSONException

class JsonGenerator(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kr.pokeum.app.api
package co.pokeum.app.api

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
Expand Down
Loading