moshi | A modern JSON library for Kotlin and Java | JSON Processing library
kandi X-RAY | moshi Summary
kandi X-RAY | moshi Summary
Moshi is a modern JSON library for Android, Java and Kotlin. It makes it easy to parse JSON into Java and Kotlin classes:.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Reads and writes the Rfc3339 dates
- Creates a new date
- Runs the game
- Main method
- Runs the application
- The main method
- Main entry point
- The main entry point
- Runs the card
- Returns the string representation of the item
- Read JSON
- Deserialize a JSON card
- Constructs a value from a JSON object
- Serializes the delegate
- Serialize a card
- The entry point
- Returns string representation
moshi Key Features
moshi Examples and Code Snippets
public class Root {
public Query query;
public String language;
public String preferedReferential;
public String bestMatch;
public List results;
public String version;
public int remainingIdentificationRequests;
@Singleton
@Provides
fun provideMoshi(): Moshi =
Moshi.Builder()
.add(KotlinJsonAdapterFactory())
.build()
@Singleton
@Provides
fun provideMoshiConverterFactory(moshi: Moshi): Mo
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/progu
val ROUTE_USER_DETAILS = "user-details/{user}"
// Pass data (I am using Moshi here)
val user = User(id = 1, name = "John Doe") // User is a data class.
val moshi = Moshi.Builder().build()
val jsonAdapter = moshi.adapter(User::class.java
Moshi moshi = new Moshi.Builder().build();
JsonAdapter adapter = moshi.adapter(Object.class);
Object jsonStructure = adapter.toJsonValue(obj);
Map jsonObject = (Map) jsonStructure;
private val moshi by lazy {
Moshi.Builder().build()
}
private val adapter by lazy {
moshi.adapter(Layout::class.java)
}
fun AssetManager.layoutsFromJson(jsonFile: String): List {
val layouts = mutableListOf()
JsonReader.o
public static final class GetConversationListResponseBody {
public final String email;
GetConversationListResponseBody(String email) {
this.email = email;
}
}
static final class InternalGetConversationListResponseBody {
Strin
Moshi moshi = new Moshi.Builder()
.add(DefaultOnDataMismatchAdapter.newFactory(Teacher.class, null))
.build();
JsonAdapter adapter = moshi.adapter(Teacher.class);
Teacher teacher = adapter.fromJson(json);
// teacher == null
public class ServletsResponse{
@Json(name = "sPK")
private Integer sPK;
@Json(name = "sName")
private String sName;
@Json(name = "cFlag")
private Integer cFlag;
public Integer getSPK() {
return sPK;
}
public void setSPK(Integer sPK) {
t
Community Discussions
Trending Discussions on moshi
QUESTION
Getting error while using Moshi code gen
for data class with Currency
.
Referred similar questions. (Adding referred question at the end)
Error
Caused by: java.lang.IllegalArgumentException: Platform class java.util.Currency requires explicit JsonAdapter to be registered
Moshi Builder
...ANSWER
Answered 2022-Apr-10 at 05:44I was able to figure out the issue.
The root cause was the default AmountJsonAdapter
created by Moshi was not internally using the CurrencyJsonAdapter
.
- Create custom JSON adapter for
Amount
rather thanCurrency
. - Add
AmountJsonAdapter()
instead ofCurrencyJsonAdapter()
inMoshi.Builder()
. - Disable moshi adapter generation for
Amount
.
Moshi Builder
QUESTION
I've been trying to make a POST with Retrofit 2 and Moshi but I've been unable to get it to work. My data classes look like this:
...ANSWER
Answered 2022-Apr-03 at 06:24In postman, you are sending data in the body of the request. But in your code, it is going as key-value params. Try to send it in the body from your code. Try below snippet.
Update your Order Data class:
QUESTION
{
"query": {
"project": "all",
"images": [
"dc5f659df9a4bcf90fc109830564d821"
],
"organs": [
"leaf"
],
"includeRelatedImages": true
},
"language": "en",
"preferedReferential": "the-plant-list",
"bestMatch": "Hibiscus rosa-sinensis L.",
"results": [
{
"score": 0.92912,
"species": {
"scientificNameWithoutAuthor": "Hibiscus rosa-sinensis",
"scientificNameAuthorship": "L.",
"genus": {
"scientificNameWithoutAuthor": "Hibiscus",
"scientificNameAuthorship": "",
"scientificName": "Hibiscus"
},
"family": {
"scientificNameWithoutAuthor": "Malvaceae",
"scientificNameAuthorship": "",
"scientificName": "Malvaceae"
},
"commonNames": [
"Hawaiian hibiscus",
"Hibiscus",
"Chinese hibiscus"
],
"scientificName": "Hibiscus rosa-sinensis L."
},
"images": [
{
"organ": "leaf",
"author": "Tela Botanica − Antoine AFFOUARD",
"license": "cc-by-sa",
"date": {
"timestamp": 1413870558000,
"string": "October 21, 2014"
},
"url": {
"o": "https://bs.plantnet.org/image/o/1d562e3be39e3297e6bdcff60d5f7d3fabe04eef",
"m": "https://bs.plantnet.org/image/m/1d562e3be39e3297e6bdcff60d5f7d3fabe04eef",
"s": "https://bs.plantnet.org/image/s/1d562e3be39e3297e6bdcff60d5f7d3fabe04eef"
},
"citation": "Tela Botanica − Antoine AFFOUARD / Pl@ntNet, cc-by-sa"
},
{
"organ": "leaf",
"author": "Antonio Sánchez Morales",
"license": "cc-by-sa",
"date": {
"timestamp": 1548682043000,
"string": "January 28, 2019"
},
"url": {
"o": "https://bs.plantnet.org/image/o/3abd84f086c90ed70306d950532f4e165f681bfe",
"m": "https://bs.plantnet.org/image/m/3abd84f086c90ed70306d950532f4e165f681bfe",
"s": "https://bs.plantnet.org/image/s/3abd84f086c90ed70306d950532f4e165f681bfe"
},
"citation": "Antonio Sánchez Morales / Pl@ntNet, cc-by-sa"
},
{
"organ": "leaf",
"author": "femenia mayra",
"license": "cc-by-sa",
"date": {
"timestamp": 1601041463219,
"string": "September 25, 2020"
},
"url": {
"o": "https://bs.plantnet.org/image/o/caa5ddc33d4b0b482b8c9f8ff8e0ba850d9af07f",
"m": "https://bs.plantnet.org/image/m/caa5ddc33d4b0b482b8c9f8ff8e0ba850d9af07f",
"s": "https://bs.plantnet.org/image/s/caa5ddc33d4b0b482b8c9f8ff8e0ba850d9af07f"
},
"citation": "femenia mayra / Pl@ntNet, cc-by-sa"
},
{
"organ": "leaf",
"author": "Antonio Sánchez Morales",
"license": "cc-by-sa",
"date": {
"timestamp": 1544800115000,
"string": "December 14, 2018"
},
"url": {
"o": "https://bs.plantnet.org/image/o/8e2c190839ebbb1bb6ab7dd26cc413bb001080f4",
"m": "https://bs.plantnet.org/image/m/8e2c190839ebbb1bb6ab7dd26cc413bb001080f4",
"s": "https://bs.plantnet.org/image/s/8e2c190839ebbb1bb6ab7dd26cc413bb001080f4"
},
"citation": "Antonio Sánchez Morales / Pl@ntNet, cc-by-sa"
},
{
"organ": "leaf",
"author": "ire110",
"license": "cc-by-sa",
"date": {
"timestamp": 1599948160933,
"string": "September 12, 2020"
},
"url": {
"o": "https://bs.plantnet.org/image/o/394caf62b3ce20c47f005d425488806f138a8493",
"m": "https://bs.plantnet.org/image/m/394caf62b3ce20c47f005d425488806f138a8493",
"s": "https://bs.plantnet.org/image/s/394caf62b3ce20c47f005d425488806f138a8493"
},
"citation": "ire110 / Pl@ntNet, cc-by-sa"
},
{
"organ": "leaf",
"author": "Andreas Vogelsinger",
"license": "cc-by-sa",
"date": {
"timestamp": 1579618800636,
"string": "January 21, 2020"
},
"url": {
"o": "https://bs.plantnet.org/image/o/cc9f39d17007bb5fd36b890cf0043a0a683352be",
"m": "https://bs.plantnet.org/image/m/cc9f39d17007bb5fd36b890cf0043a0a683352be",
"s": "https://bs.plantnet.org/image/s/cc9f39d17007bb5fd36b890cf0043a0a683352be"
},
"citation": "Andreas Vogelsinger / Pl@ntNet, cc-by-sa"
}
],
"gbif": {
"id": "3152559"
}
},
{
"score": 0.03652,
"species": {
"scientificNameWithoutAuthor": "Hibiscus schizopetalus",
"scientificNameAuthorship": "(Dyer) Hook.f.",
"genus": {
"scientificNameWithoutAuthor": "Hibiscus",
"scientificNameAuthorship": "",
"scientificName": "Hibiscus"
},
"family": {
"scientificNameWithoutAuthor": "Malvaceae",
"scientificNameAuthorship": "",
"scientificName": "Malvaceae"
},
"commonNames": [
"Campanilla",
"Chinese lantern",
"Japanese hibiscus"
],
"scientificName": "Hibiscus schizopetalus (Dyer) Hook.f."
},
"images": [
{
"organ": "leaf",
"author": "nanni pri",
"license": "cc-by-sa",
"date": {
"timestamp": 1619808048797,
"string": "April 30, 2021"
},
"url": {
"o": "https://bs.plantnet.org/image/o/4ca16c7045b6b2e56cbbc0f4afed08ddc3f556f7",
"m": "https://bs.plantnet.org/image/m/4ca16c7045b6b2e56cbbc0f4afed08ddc3f556f7",
"s": "https://bs.plantnet.org/image/s/4ca16c7045b6b2e56cbbc0f4afed08ddc3f556f7"
},
"citation": "nanni pri / Pl@ntNet, cc-by-sa"
},
{
"organ": "leaf",
"author": "sharlaine bustamants",
"license": "cc-by-sa",
"date": {
"timestamp": 1591247217009,
"string": "June 4, 2020"
},
"url": {
"o": "https://bs.plantnet.org/image/o/c4722a732fef3bfd1eab6d6d80b500bb036e5215",
"m": "https://bs.plantnet.org/image/m/c4722a732fef3bfd1eab6d6d80b500bb036e5215",
"s": "https://bs.plantnet.org/image/s/c4722a732fef3bfd1eab6d6d80b500bb036e5215"
},
"citation": "sharlaine bustamants / Pl@ntNet, cc-by-sa"
},
{
"organ": "leaf",
"author": "Hugo SANTACREU",
"license": "cc-by-sa",
"date": {
"timestamp": 1531233524000,
"string": "July 10, 2018"
},
"url": {
"o": "https://bs.plantnet.org/image/o/3ddba92c55e30e0cb70b10feb1cd021190dfec76",
"m": "https://bs.plantnet.org/image/m/3ddba92c55e30e0cb70b10feb1cd021190dfec76",
"s": "https://bs.plantnet.org/image/s/3ddba92c55e30e0cb70b10feb1cd021190dfec76"
},
"citation": "Hugo SANTACREU / Pl@ntNet, cc-by-sa"
},
{
"organ": "leaf",
"author": "Pierre Bonnet",
"license": "cc-by-sa",
"date": {
"timestamp": 1534366939000,
"string": "August 15, 2018"
},
"url": {
"o": "https://bs.plantnet.org/image/o/f68ad5f10d76837924537d560cbb62b6cb4003c5",
"m": "https://bs.plantnet.org/image/m/f68ad5f10d76837924537d560cbb62b6cb4003c5",
"s": "https://bs.plantnet.org/image/s/f68ad5f10d76837924537d560cbb62b6cb4003c5"
},
"citation": "Pierre Bonnet / Pl@ntNet, cc-by-sa"
},
{
"organ": "leaf",
"author": "Tobias Jacobsen",
"license": "cc-by-sa",
"date": {
"timestamp": 1571130043838,
"string": "October 15, 2019"
},
"url": {
"o": "https://bs.plantnet.org/image/o/44da8a021cfe8dcc1004a2998ff13982cce545f3",
"m": "https://bs.plantnet.org/image/m/44da8a021cfe8dcc1004a2998ff13982cce545f3",
"s": "https://bs.plantnet.org/image/s/44da8a021cfe8dcc1004a2998ff13982cce545f3"
},
"citation": "Tobias Jacobsen / Pl@ntNet, cc-by-sa"
},
{
"organ": "leaf",
"author": "abderrahim Lamari",
"license": "cc-by-sa",
"date": {
"timestamp": 1619785453625,
"string": "April 30, 2021"
},
"url": {
"o": "https://bs.plantnet.org/image/o/e669d09a54681d3d56fabfb588d3ecb50b523490",
"m": "https://bs.plantnet.org/image/m/e669d09a54681d3d56fabfb588d3ecb50b523490",
"s": "https://bs.plantnet.org/image/s/e669d09a54681d3d56fabfb588d3ecb50b523490"
},
"citation": "abderrahim Lamari / Pl@ntNet, cc-by-sa"
}
],
"gbif": {
"id": "9064581"
}
}
],
"version": "2022-02-14 (5.0)",
"remainingIdentificationRequests": 491
}
...ANSWER
Answered 2022-Mar-31 at 08:46Here I am sharing "Deserialize your JSON string into Java objects in four different ways". There are a lot of other java JSON libraries.
1. Using Moshi
2. Using Jackson
3. Using Gson
4. Using Genson
"Remember - The POJOs in java are useful in defining objects to increase their readability and reusability."
We'll use the following Root class with multiple fields as the object to deserialize throughout.
Root.javaQUESTION
I am working on an app that interfaces with an API to select from and update a MySQL database.
I am using Kotlin with Retrofit and the Moshi converter. (As I am new to learning all this stuff, this app is modified from the Mars Photo code on https://developer.android.com/courses/pathways/android-basics-kotlin-unit-4-pathway-2 .)
I have successfully implemented the GET part in Retrofit thanks to building out from the above tutorial, but I am having a heck of a time POSTing to the database.
The POST to update via the API works in Postman but I have tried multiple ways to replicate that in my app without success. I have tried a pretty basic POST passing in my data object (as standard Kotlin vars as well as Json annotated or SerializedName annotated), and I have tried Formurlencoded with Fields and Multipart with Parts.
I am including what I hope is all the relevant code (the latest attempt, at least). Thanks for any assistance!
...ANSWER
Answered 2022-Mar-24 at 11:42Normally you create your @Body
classes as :
QUESTION
We are having problems with Android network requests, to be more exact receiving random
...ANSWER
Answered 2022-Feb-25 at 10:33You can add code below to pro-guard file:
QUESTION
I'm using Jetpack Compose + Navigation (Single Activity, no Fragments) and i'm trying to perform a navigation route as follow:
SplashScreen
---(delay)---> AuthScreen
---(if successful)--> MainScreen
Unfortunately, when i perform Login, the function navigate()
in the LoginScreen
composables causes an infinite loop. I don't understand if i'm triggering recomposition or what happens.
Unfortunately, it's hard to share all the code, but keep in mind that:
- the issue doesn't seem to be related to
LoginScreen
andMainScreen
composables (you can assume them to be just a simpleText
composable) - It doesn't seem to be related to
NavigationGraph
too. Infact, if i just make theSplashScreen
-->MainScreen
transition there is no problem occurring - If i remove the line
navController.navigate("main")
there is no more loop; - The code is based (almost copy-paste) of this example
This is the AuthScreen
code where the issue occurs.
ANSWER
Answered 2022-Feb-22 at 17:35It's because you're trying to navigate from a composable. See the documentation
You should only call navigate() as part of a callback and not as part of your composable itself, to avoid calling navigate() on every recomposition.
You could use a LaunchEffect
for instance
QUESTION
Hey I am trying to run my application and I am getting this error
build.gradle
...ANSWER
Answered 2021-Nov-19 at 06:20Dependency: androidx.lifecycle:lifecycle-runtime-ktx:2.4.0. The issue is with this dependency, there may be some transitive dependency error. I would suggest use the alpha version of this dependency, worked for me. Replace with this dependency : androidx.lifecycle:lifecycle-*:2.4.0-alpha03
QUESTION
I have not very well structured JSON from BE (that I cannot change) which was formerly handled with moshi (custom adapter for the issue). Now I am trying to use pure kotlin serialization instead, but as said, JSON structure doesn't help me much.
...ANSWER
Answered 2022-Feb-01 at 11:06Having classDiscriminator
is not a mandatory requirement for polymorphic deserialization. You can use content-based polymorphic deserialization in this case.
All you need is to define a JsonContentPolymorphicSerializer
for Values
class with some logic for exact subclass serializer selection:
QUESTION
After updating my Android app from Kotlin 1.5.21 to Kotlin 1.6.0, my proguard rules stopped working (i.e. they are ignored). This caused some reflection code to break on release mode. Downgrading to Kotlin 1.5.21 fixes the problem.
Are there any known problems with Kotlin 1.6.0 and proguard ?
Googling around, I see that there are other related problems (see this, this, this).
...ANSWER
Answered 2022-Jan-31 at 10:09The problem was the version of R8 bundled with the Android Gradle Plugin (AGP).
Following the advice in this related answer, I explicitly set com.android.tools:r8:3.0.77
in my gradle configuration file.
QUESTION
Retrofit and moshi-kotlin Api's are not working in android.
I'm using dagger2 ,while genrating signed apk ,retrofit api calls are not working, in debug mode its working fine .
while we enabled minifyenable true and shrinkresource true.
@JsonClass(generateAdapter = true) @SuppressLint("ParcelCreator") @Keep data class DashboardListRes( @Json(name = "data") var
data: List?=ArrayList(), @Json(name = "lead_data") var leadData: LeadData= LeadData(), @Json(name = "message") var message: String="", @Json(name = "status") var status: String="", @Json(name = "status_code") var statusCode: Int=0, @Json(name = "user") var user: User=User() ):Serializable
1. 2. -keepattributes Signature, InnerClasses, EnclosingMethod
...ANSWER
Answered 2022-Jan-31 at 07:38If you are converting the data received into a POJO object using some built in function and then populating a data class or something, you need to make sure you exclude the data classes from proguard.
What I generally do is I put all my data classes in one package and then exclude everything in that package like so:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install moshi
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page