markomilos / jsonapi Goto Github PK
View Code? Open in Web Editor NEWLibrary for streamlined use of JSON:API using Kotlin and Java built on top of a modern json library Moshi.
License: Apache License 2.0
Library for streamlined use of JSON:API using Kotlin and Java built on top of a modern json library Moshi.
License: Apache License 2.0
Hi,
I am having trouble parsing back the JSON:API I write with jsonapi.
This is the JSON.
{"meta":{"created":"2023-07-31T13:09:13Z"},"links":{"self":"/path/85699a3b-fe81-41bc-aae0-9ea4aa0b15ca"},"data":{"type":"job","relationships":{"files":{"data":[{"type":"file","id":"export.csv"}]}},"attributes":{"status":"Completed","jobId":"85699a3b-fe81-41bc-aae0-9ea4aa0b15ca"}},"included":[{"type":"file","id":"export.csv","attributes":{"filename":"export.csv","location":"oci://path/export.csv"}}]}
all code is basically a copy/paste from the README examples. I am getting this error, in one project:
com.squareup.moshi.JsonDataException: Required value 'id' missing at $.included[0].attributes
and I also get a similar error in another, with a different JSON:
Required value 'id' missing at $.data[0].attributes
any idea why it is happening?
I'm following the README documentation and I'm confused about what to pass for type:
Create adapter for Document type:
val adapter = moshi.adapter<Document
>(type)val type = Document::class.java
val adapter = moshi.adapter<Document>(type)
complains with:
Exception in thread "main" java.lang.IllegalArgumentException: No JsonAdapter for T (with no annotations)
val type = Account::class.java
val adapter = moshi.adapter<Document>(type)
Seems to work, but when I run:
val document = adapter.fromJson(response)
It crashes with:
Exception in thread "main" jsonapi.JsonFormatException: A resource identifier MUST contain non-null/non-empty type member but it was not found on path: $
Iām trying to create a document from a request which returns an empty response when the request succeeds but when there is an error it returns a HTTP 401 with the error in json. The problem is iām not getting a document when the server returns a 401. It only returns a retrofit HttpException with a response with an error body at that moment.
The network call looks like this:
@POST("authenticate")
fun authenticate(): Single<Document<Void>>
The error body looks like this:
{
"errors": [
{
"id": "",
"status": "401",
"code": "invalid_credentials",
"title": "Authentication Failed",
"detail": "Invalid credentials"
}
]
}
Is there anything we are doing wrong? We are using retrofit with RxJava
Not an issue but rather a question.
Is there any plans to support Atomic Operations in the future?
Thanks
Don't get me wrong, Moshi is great and so is this project, but would it be possible or would you be willing to allow splitting the jsonapi-adapters
subproject into jsonapi-core
and jsonapi-adapters
? The idea being to make Moshi one of many implementations. It's easy enough to use this library in a Ktor project via the content negotiation plugin. I would love to be able to use this directly in my Spring Boot projects as well, but that unfortunately requires Jackson (š¤® ). Thoughts?
Suppose I have a model with a relationship, an instance of that model, and a JSON API representation of that instance without any included resources. The instance has a relationship with a target object. The JSON API representation specifies the type and id of the target resource of the relationship. All of this works, I can correctly create the JSON API document and persist it.
But then when I retrieve the JSON API representation, the instance created has the relationship set to null. This seems odd to me. There is enough information to do better. The relationship target is not included, but since the type and id are present, the relationship target should be created with the id. Then the full target resource can be retrieved later if needed. Also the relationship object really needs to be represented in Document. Since it isn't, fixing this requires a lot of extra work.
Hello!
Is it possible to make this library work with Moshi codegen?
According to the 1.1 specification LID
MAY be present but is not really required for client side created resources
reference:
https://jsonapi.org/format/1.1/#document-resource-objects
Are there any plans to implement incremental processing for the annotation library?
The following annotation processors are not incremental: jsonapi-compiler-1.0.0.jar (com.markomilos.jsonapi:jsonapi-compiler:1.0.0).
Make sure all annotation processors are incremental to improve your build speed.
Hi!
How do I serialize collection of resources?
Here's what I'm trying:
override fun serializeMany(document: Document<*>): String {
document as Document<List<AppResource>>
return moshi.adapter<Document<List<AppResource>>>(AppResource::class.java).toJson(document)
}
I get this exception at runtime:
java.lang.IllegalArgumentException: A resource MUST contain non-null, non-empty type.
at jsonapi.internal.ResourceBinder.readType(ResourceBinder.kt:55)
at jsonapi.internal.ResourceBinder.readResourceObject(ResourceBinder.kt:34)
at jsonapi.internal.adapter.ResourceTypeAdapter.toJson(ResourceTypeAdapter.kt:105)
at com.squareup.moshi.JsonAdapter.toJson(JsonAdapter.java:82)
at com.squareup.moshi.JsonAdapter.toJson(JsonAdapter.java:90)
at api.controller.AppsController.serializeMany(AppsController.kt:33)
at api.controller.Controller.getMany(Controller.kt:15)
at api.Router$route$2.invokeSuspend(Router.kt:37)
at api.Router$route$2.invoke(Router.kt)
at api.Router$route$2.invoke(Router.kt)
Hey,
I wanted to find out if it is possible to include the serialization of any null values. Currently, if we have a null value in our object, then the field is not created in the JSON:
Our object currently looks like this:
@Resource("item-summary")
data class TransactionResponse(
@Id val id: String,
val title: String?,
val subtitle: String?,
val datetime: OffsetDateTime,
val customerId: String,
)
If the title and subtitle are null then we get the following JSON:
{
"data": [
{
"type": "item-summary",
"id": "1",
"attributes": {
"datetime": "2022-08-17T10:15:45.778832+02:00",
"customerId": "12",
}
}
]
}
Where we would like it to be:
{
"data": [
{
"type": "item-summary",
"id": "1",
"attributes": {
"title":null,
"subtitle":null,
"datetime": "2022-08-17T10:15:45.778832+02:00",
"customerId": "12",
}
}
]
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
š Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ššš
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ā¤ļø Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.