Git Product home page Git Product logo

jsonapi's People

Contributors

markomilos avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

jsonapi's Issues

can't parse back from JSON

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?

What do I pass for type?

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: $

Problem creating error document

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

Possible to decouple Moshi?

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?

Relationship are ignored when targets are not included

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.

Moshi Codegen

Hello!

Is it possible to make this library work with Moshi codegen?

Incremental annotation processor

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.

Serialize collection of resources

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)

Serialize Null values

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",
            }
        }
    ]
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    šŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. šŸ“ŠšŸ“ˆšŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ā¤ļø Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.