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

Kotlin support for index access operators #1844

Closed
wants to merge 4 commits into from
Closed

Conversation

Bixilon
Copy link

@Bixilon Bixilon commented Jan 5, 2021

In Kotlin you can define your own operators. You can use the ::set Method to add a object. See https://kotlinlang.org/docs/reference/keyword-reference.html for more.

I added the set method in JsonObject. It is a simple alias to add or addProperty.

Also I need the current String position in JsonReader. E.g I have a command as a string and the next argument is a JsonObject. I just want to know how many chars were read from the input string to determinate the next argument. Currently this looks like this:

    private static final Field JSON_READER_POS_FIELD;
    private static final Field JSON_READER_LINE_START_FIELD;

        new JsonReader(new StringReader(""));
        Class<?> jsonReadClass = JsonReader.class;
        try {
            JSON_READER_POS_FIELD = jsonReadClass.getDeclaredField("pos");
            JSON_READER_POS_FIELD.setAccessible(true);
            JSON_READER_LINE_START_FIELD = jsonReadClass.getDeclaredField("lineStart");
            JSON_READER_LINE_START_FIELD.setAccessible(true);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }

        try {
            return JSON_READER_POS_FIELD.getInt(jsonReader) - JSON_READER_LINE_START_FIELD.getInt(jsonReader) + 1;
        } catch (IllegalAccessException e) {
            throw new IllegalStateException();
        }

@google-cla google-cla bot added the cla: yes label Jan 5, 2021
Copy link
Collaborator

@Marcono1234 Marcono1234 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I am not sure whether adding more methods with different names but the same behavior as existing methods is really worth it, especially since the main target of this project seems to be Java. Have a look at https://github.com/square/moshi which is designed for Kotlin.

I have added some review comments, but it might not be worth it considering them because this project does not seem to be actively maintained anymore, see #1821.


Note that I am not a maintainer of this project.

@Bixilon
Copy link
Author

Bixilon commented Jan 9, 2021

I don't know if it is still maintained, but still making this PR. Everything is resolved now.

@Marcono1234 Marcono1234 added the kotlin Issues concerning the use of Gson with Kotlin label Mar 1, 2024
@eamonnmcmanus
Copy link
Member

Reviving this conversation. 🙂
This project is now maintained. However I agree with @Marcono1234 that syntactic convenience in Kotlin isn't a good enough reason to add a second name to existing methods. You can easily use Kotlin extension functions to achieve the same thing in your project:

operator fun JsonObject.set(property: String, value: JsonElement) = add(property, value)
operator fun JsonObject.set(property: String, value: String) = addProperty(property, value)
operator fun JsonObject.set(property: String, value: Number) = addProperty(property, value)
operator fun JsonObject.set(property: String, value: Boolean) = addProperty(property, value)
operator fun JsonObject.set(property: String, value: Char) = addProperty(property, value)

It would be good if there were a standard artifact to house extension functions like these. I don't think the main Gson artifact (com.google.code.gson:gson) would be the right place, though, because it would probably have to acquire a dependency on the Kotlin runtime.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes kotlin Issues concerning the use of Gson with Kotlin
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants