|  | 
|  | 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. | 
0 commit comments