Skip to content

Commit fa5469e

Browse files
Merge pull request #1 from shubhamrapido/main
Adding initial version of sdk
2 parents 918c434 + cb9f9ac commit fa5469e

File tree

84 files changed

+3392
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+3392
-0
lines changed

.gitignore

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
*.iml
2+
.kotlin
3+
.gradle
4+
**/build/
5+
xcuserdata
6+
!src/**/build/
7+
local.properties
8+
.idea
9+
.DS_Store
10+
captures
11+
.externalNativeBuild
12+
.cxx
13+
*.xcodeproj/*
14+
!*.xcodeproj/project.pbxproj
15+
!*.xcodeproj/xcshareddata/
16+
!*.xcodeproj/project.xcworkspace/
17+
!*.xcworkspace/contents.xcworkspacedata
18+
**/xcshareddata/WorkspaceSettings.xcsettings

README.md

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
# KMP JSON Reader
2+
3+
A Kotlin Multiplatform library for reading and parsing JSON files across Android and iOS platforms.
4+
5+
## Features
6+
7+
- Read JSON files from various sources:
8+
- Android: Assets directory and file system
9+
- iOS: Bundle resources and file system
10+
- Parse JSON strings to Map structures
11+
- Parse JSON to specific types (Android only)
12+
- Clean architecture with separation of concerns
13+
- Comprehensive error handling
14+
15+
## Installation
16+
17+
### Gradle Setup (build.gradle.kts)
18+
19+
```kotlin
20+
repositories {
21+
mavenCentral()
22+
}
23+
24+
dependencies {
25+
// For multiplatform projects
26+
implementation("com.backpackerdevs:jsonreader:1.0.0")
27+
28+
// For Android-only projects
29+
implementation("com.backpackerdevs:jsonreader-android:1.0.0")
30+
31+
// For iOS-only projects
32+
implementation("com.backpackerdevs:jsonreader-ios:1.0.0")
33+
}
34+
```
35+
36+
## Setup
37+
38+
### Android
39+
40+
Initialize the JsonReader in your Application class:
41+
42+
```kotlin
43+
class MyApplication : Application() {
44+
override fun onCreate() {
45+
super.onCreate()
46+
JsonReaderImpl.initialize(this)
47+
}
48+
}
49+
```
50+
51+
### iOS
52+
53+
No additional setup is required for iOS.
54+
55+
## Usage
56+
57+
### Creating a JsonReader Instance
58+
59+
```kotlin
60+
// Get a platform-specific implementation
61+
val jsonReader = JsonReaderFactory.create()
62+
```
63+
64+
### Reading a JSON File
65+
66+
```kotlin
67+
// Read a JSON file (works on both Android and iOS)
68+
jsonReader.readJsonFromFile("sample.json")
69+
.collect { result ->
70+
when (result) {
71+
is JsonResult.Loading -> {
72+
// Show loading state
73+
}
74+
is JsonResult.Success -> {
75+
val jsonContent = result.data
76+
// Process the JSON content
77+
}
78+
is JsonResult.Error -> {
79+
val errorMessage = result.message
80+
// Handle error
81+
}
82+
}
83+
}
84+
```
85+
86+
### Parsing JSON to a Map
87+
88+
```kotlin
89+
val jsonString = """{"name": "John", "age": 30}"""
90+
val jsonMap = jsonReader.parseJson(jsonString)
91+
92+
// Access values
93+
val name = jsonMap["name"] as String
94+
val age = jsonMap["age"] as Int
95+
```
96+
97+
### Parsing JSON to a Specific Type (Android Only)
98+
99+
```kotlin
100+
data class Person(val name: String, val age: Int)
101+
102+
val jsonString = """{"name": "John", "age": 30}"""
103+
val person = jsonReader.parseJsonToType(jsonString, "com.example.Person") as Person
104+
```
105+
106+
## API Reference
107+
108+
### JsonReader Interface
109+
110+
The main interface for the library:
111+
112+
```kotlin
113+
interface JsonReader {
114+
fun readJsonFromFile(path: String): Flow<JsonResult<String>>
115+
fun parseJson(jsonString: String): Map<String, Any?>
116+
fun parseJsonToType(jsonString: String, typeName: String): Any
117+
}
118+
```
119+
120+
### JsonResult Class
121+
122+
A sealed class representing the result of a JSON operation:
123+
124+
```kotlin
125+
sealed class JsonResult<out T> {
126+
data class Success<T>(val data: T) : JsonResult<T>()
127+
data class Error(val message: String, val exception: Throwable? = null) : JsonResult<Nothing>()
128+
data object Loading : JsonResult<Nothing>()
129+
}
130+
```
131+
132+
## Platform-Specific Details
133+
134+
### Android
135+
136+
- Files can be read from the assets directory or the file system
137+
- JSON can be parsed to specific types using reflection (via Gson)
138+
139+
### iOS
140+
141+
- Files can be read from the main bundle or the file system
142+
- The `parseJsonToType` method is not supported on iOS and will throw an `UnsupportedOperationException`
143+
144+
## License
145+
146+
This library is licensed under the MIT License - see the LICENSE file for details.
147+
148+
## Contributing
149+
150+
Contributions are welcome! Please feel free to submit a Pull Request.

build.gradle.kts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
plugins {
2+
// this is necessary to avoid the plugins to be loaded multiple times
3+
// in each subproject's classloader
4+
alias(libs.plugins.androidApplication) apply false
5+
alias(libs.plugins.androidLibrary) apply false
6+
alias(libs.plugins.composeMultiplatform) apply false
7+
alias(libs.plugins.composeCompiler) apply false
8+
alias(libs.plugins.kotlinMultiplatform) apply false
9+
}

composeApp/build.gradle.kts

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import org.jetbrains.compose.desktop.application.dsl.TargetFormat
2+
import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi
3+
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
4+
5+
plugins {
6+
alias(libs.plugins.kotlinMultiplatform)
7+
alias(libs.plugins.androidApplication)
8+
alias(libs.plugins.composeMultiplatform)
9+
alias(libs.plugins.composeCompiler)
10+
alias(libs.plugins.kotlinxSerialization)
11+
}
12+
13+
kotlin {
14+
androidTarget {
15+
@OptIn(ExperimentalKotlinGradlePluginApi::class)
16+
compilerOptions {
17+
jvmTarget.set(JvmTarget.JVM_11)
18+
}
19+
}
20+
21+
listOf(
22+
iosX64(),
23+
iosArm64(),
24+
iosSimulatorArm64()
25+
).forEach { iosTarget ->
26+
iosTarget.binaries.framework {
27+
baseName = "ComposeApp"
28+
isStatic = true
29+
}
30+
}
31+
32+
sourceSets {
33+
34+
androidMain.dependencies {
35+
implementation(compose.preview)
36+
implementation(libs.androidx.activity.compose)
37+
implementation(libs.koin.android)
38+
implementation(libs.koin.compose)
39+
}
40+
commonMain.dependencies {
41+
implementation(compose.runtime)
42+
implementation(compose.foundation)
43+
implementation(compose.material3)
44+
implementation(compose.ui)
45+
implementation(compose.components.resources)
46+
implementation(compose.components.uiToolingPreview)
47+
implementation(libs.androidx.lifecycle.viewmodel)
48+
implementation(libs.androidx.lifecycle.runtimeCompose)
49+
50+
// JSON Reader Library
51+
implementation(project(":jsonreader"))
52+
53+
// Coroutines
54+
implementation(libs.kotlinx.coroutines.core)
55+
56+
// Koin
57+
implementation(libs.koin.core)
58+
implementation(libs.koin.compose.multiplatform)
59+
60+
// Serialization
61+
implementation(libs.kotlinx.serialization.json)
62+
}
63+
commonTest.dependencies {
64+
implementation(libs.kotlin.test)
65+
}
66+
}
67+
}
68+
69+
android {
70+
namespace = "com.backpackerdevs.jsonreader"
71+
compileSdk = libs.versions.android.compileSdk.get().toInt()
72+
73+
defaultConfig {
74+
applicationId = "com.backpackerdevs.jsonreader"
75+
minSdk = libs.versions.android.minSdk.get().toInt()
76+
targetSdk = libs.versions.android.targetSdk.get().toInt()
77+
versionCode = 1
78+
versionName = "1.0"
79+
}
80+
packaging {
81+
resources {
82+
excludes += "/META-INF/{AL2.0,LGPL2.1}"
83+
}
84+
}
85+
buildTypes {
86+
getByName("release") {
87+
isMinifyEnabled = false
88+
}
89+
}
90+
compileOptions {
91+
sourceCompatibility = JavaVersion.VERSION_11
92+
targetCompatibility = JavaVersion.VERSION_11
93+
}
94+
95+
lint {
96+
disable += "NullSafeMutableLiveData"
97+
abortOnError = false
98+
}
99+
}
100+
101+
dependencies {
102+
debugImplementation(compose.uiTooling)
103+
}
104+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
3+
4+
<application
5+
android:name=".JsonReaderApplication"
6+
android:allowBackup="true"
7+
android:icon="@mipmap/ic_launcher"
8+
android:label="@string/app_name"
9+
android:roundIcon="@mipmap/ic_launcher_round"
10+
android:supportsRtl="true"
11+
android:theme="@android:style/Theme.Material.Light.NoActionBar">
12+
<activity
13+
android:exported="true"
14+
android:name=".MainActivity">
15+
<intent-filter>
16+
<action android:name="android.intent.action.MAIN" />
17+
18+
<category android:name="android.intent.category.LAUNCHER" />
19+
</intent-filter>
20+
</activity>
21+
22+
<provider
23+
android:name="androidx.core.content.FileProvider"
24+
android:authorities="${applicationId}.fileprovider"
25+
android:exported="false"
26+
android:grantUriPermissions="true">
27+
<meta-data
28+
android:name="android.support.FILE_PROVIDER_PATHS"
29+
android:resource="@xml/file_paths" />
30+
</provider>
31+
</application>
32+
33+
</manifest>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"title": "Sample JSON File",
3+
"description": "This is a sample JSON file for testing the KMP JSON Reader library",
4+
"items": [
5+
{
6+
"id": 1,
7+
"name": "Item 1",
8+
"description": "First test item"
9+
},
10+
{
11+
"id": 2,
12+
"name": "Item 2",
13+
"description": "Second test item"
14+
},
15+
{
16+
"id": 3,
17+
"name": "Item 3",
18+
"description": "Third test item"
19+
}
20+
],
21+
"metadata": {
22+
"created": "2024-07-04",
23+
"version": "1.0.0",
24+
"author": "KMP JSON Reader"
25+
}
26+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.backpackerdevs.jsonreader
2+
3+
import android.app.Application
4+
import com.backpackerdevs.jsonreader.di.initKoin
5+
6+
class JsonReaderApplication : Application() {
7+
override fun onCreate() {
8+
super.onCreate()
9+
10+
// Initialize Koin at application startup
11+
initKoin(applicationContext)
12+
13+
// Initialize JsonReaderImpl
14+
JsonReaderImpl.initialize(this)
15+
}
16+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.backpackerdevs.jsonreader
2+
3+
import android.os.Bundle
4+
import androidx.activity.ComponentActivity
5+
import androidx.activity.compose.setContent
6+
import androidx.activity.enableEdgeToEdge
7+
import androidx.compose.runtime.Composable
8+
import androidx.compose.ui.tooling.preview.Preview
9+
10+
class MainActivity : ComponentActivity() {
11+
override fun onCreate(savedInstanceState: Bundle?) {
12+
enableEdgeToEdge()
13+
super.onCreate(savedInstanceState)
14+
15+
setContent {
16+
App()
17+
}
18+
}
19+
}
20+
21+
@Preview
22+
@Composable
23+
fun AppAndroidPreview() {
24+
App()
25+
}

0 commit comments

Comments
 (0)