Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supporting JsonPointer? #69

Open
inder123 opened this issue Aug 8, 2015 · 4 comments
Open

Supporting JsonPointer? #69

inder123 opened this issue Aug 8, 2015 · 4 comments
Milestone

Comments

@inder123
Copy link

inder123 commented Aug 8, 2015

https://tools.ietf.org/html/rfc6901

Any thoughts on support JsonPointer for self-referencing values in a JSON doc? JSR-367 is planning to support it.

@swankjesse
Copy link
Collaborator

We've got JsonPath and this is isomorphic. I have no preference but we should pick the winner!

@swankjesse swankjesse added this to the Icebox milestone Oct 14, 2016
@ghostbuster91
Copy link

ghostbuster91 commented Jan 23, 2018

I've just tried to create a naive implementation of JsonPath in moshi using annotations but I think it is not possible right now. Just after I add an attribute to my annotation moshi stops matching the adapter.

In casy anybody would be interested in pushing this further, here is my code:

class JsonPathFactory : JsonAdapter.Factory {
    override fun create(type: Type?, annotations: MutableSet<out Annotation>, moshi: Moshi): JsonAdapter<*>? {
        val jsonPathAnnotation = annotations.firstOrNull { it.annotationClass == JsonPath::class }
        if (jsonPathAnnotation != null) {
            val path = Proxy.getInvocationHandler(jsonPathAnnotation).invoke(jsonPathAnnotation, JsonPath::class.java.declaredMethods.first { it.name == "path" }, arrayOf()) as String
            return object : JsonAdapter<String>() {
                
                @JsonPath()
                override fun fromJson(reader: JsonReader): String? {
                        @Suppress("UNCHECKED_CAST")
                        val map = reader.readJsonValue() as Map<String, Any>
                        var currentNode = map
                        val pathSegments = path.split("/")
                        pathSegments.dropLast(1).forEach {
                            @Suppress("UNCHECKED_CAST")
                            currentNode = currentNode[it] as Map<String, Any>
                        }
                        return currentNode[pathSegments.last()] as String
                }

                override fun toJson(writer: JsonWriter?, @JsonPath value: String?) = Unit
            }
        }
        return null
    }
}


@Retention(AnnotationRetention.RUNTIME)
@JsonQualifier
annotation class JsonPath(val path: String = "")

@DanielNogueiraAndroid
Copy link

Hey, any progress on the implementation of the JsonPointer?

@JakeWharton
Copy link
Collaborator

JakeWharton commented Aug 19, 2019 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants