https://kotlinlang.org logo
Join SlackCommunities
Powered by
# kotson
  • m

    macmedan

    06/26/2017, 2:18 PM
    And am struggling to figure out how to convert this to #kotson
  • s

    salomonbrys

    06/26/2017, 2:27 PM
    Kotson IS Gson. There are no additional special structures.
  • m

    macmedan

    06/26/2017, 2:32 PM
    So why even have the library if all it is, is the library already provided by Gson
  • s

    salomonbrys

    06/26/2017, 2:37 PM
    Kotson provides a lot of utility functions that makes the use of Gson in Kotlin a lot easier.
  • s

    salomonbrys

    06/26/2017, 2:38 PM
    Everything is documented here : https://github.com/SalomonBrys/Kotson/
  • m

    macmedan

    06/28/2017, 2:49 AM
    Thanks for your help @salomonbrys it seems I was just trying to do too much work. Once I deleted all my
    TypeAdapters
    things started working. Just me not understanding what was happening.
  • k

    ksaltykov

    08/03/2017, 7:12 AM
    How I can map object with format like this?
    Copy code
    [{"type": "photo", "photo": {"id": 123}}, {"type": "video", "video": {"id": 321}}]
  • k

    ksaltykov

    08/03/2017, 7:20 AM
    And how I can map String to enum?
  • s

    salomonbrys

    08/05/2017, 9:46 AM
    @kiril To map a Json that has a "fluent" schema, you should use custom type adapters String to enum mapping is automatic in Gson
  • c

    carlbenson

    10/05/2017, 3:59 PM
    question: I have a
    json
    with the following structure
    Copy code
    {
      "marketId1": {
        "services": {
          "savings": true,
          "funds": true
        },
        "paymentMethod": "autogiro",
        "withdrawalMethod": "autogiro",
        "bankProvider": {
          "name": "bank1",
          "offersMarketing": false
        },
        "fundsProvider": {
          "name": "bank1 funds"
        },
        "onboarding": {
          "kyc": "embeddedInWebview",
          "strongAuth": "embeddedInWebview"
        },
        "strongAuthenticationMethod": {
          "name": "strongAuth1",
          "method": "externalApp",
          "externalAppURL": "scheme1://"
        }
      },
      "marketId2": {
        "services": {
          "savings": true
        },
        "paymentMethod": "debitCard",
        "withdrawalMethod": "bankAccount",
        "bankProvider": {
          "name": "bank2",
          "offersMarketing": false
        },
        "onboarding": {
          "kyc": "preOnboarding",
          "strongAuth": "preOnboarding"
        },
        "strongAuthenticationMethod": {
          "name": "strongAuth2",
          "method": "internalBrowser"
        }
      }
    }
  • c

    carlbenson

    10/05/2017, 4:01 PM
    I’m construction a
    Capabilities
    object using kotson delegates like so
    Copy code
    class Capabilities(val obj: JsonObject, market: String) {
        val hasSavings by obj[market]["services"].byBool("savings")
        internal val hasFundsMaybe by obj[market]["services"].byNullableBool("funds")
        val paymentMethod by obj[market].byString("paymentMethod")
        val withdrawalMethod by obj[market].byString("withdrawalMethod")
        val bankProviderName by obj[market]["bankProvider"].byString("name")
        val fundProviderName by obj[market]["fundsProvider"].byString("name")
        val onboardingKyc by obj[market]["onboarding"].byString("kyc")
        val strongAuth by obj[market]["onboarding"].byString("strongAuth")
        val strongAuthMethod by obj[market]["strongAuthenticationMethod"].byString("method")
    }
    
    val Capabilities.hasBankAccountWithdrawalMethod: Boolean get() = withdrawalMethod == "bankAccount"
    val Capabilities.isOnboardingKycEmbedded: Boolean get() = onboardingKyc == "embeddedInWebview"
    val Capabilities.isStrongAuthEmbedded: Boolean get() = strongAuth == "embeddedInWebView"
    val Capabilities.isPaymentMethodDebitCard: Boolean get() = paymentMethod == "debitCard"
    val Capabilities.isStrongAuthMethodInternal: Boolean get() = strongAuthMethod == "internalBrowser"
    val Capabilities.hasFunds: Boolean get() = hasFundsMaybe == true
  • c

    carlbenson

    10/05/2017, 4:02 PM
    but for
    marketId2
    I cannot create the object, since it does not contain a
    fundsProvider
    object at all …
  • c

    carlbenson

    10/05/2017, 4:03 PM
    I would like avoid exposing nullables as far as possible, hence the
    val Capabilities.hasFunds: Boolean get() = hasFundsMaybe == true
    and
    internal val hasFundsMaybe by obj[market]["services"].byNullableBool("funds")
  • c

    carlbenson

    10/05/2017, 4:14 PM
    so my question is: how do I use delegates to be able to create a
    Capabilities
    object even for
    marketId2
    ?
  • c

    carlbenson

    10/05/2017, 4:25 PM
    maybe it is not solvable using delegates? this seemed to work
    val fundProviderName: String get() = obj[market].get("fundsProvider").nullObj?.get("name")?.nullString ?: ""
  • c

    carlbenson

    10/05/2017, 4:32 PM
    Actually, I think I’ll settle on this in the
    Capabilities
    class
    val fundProvider by obj[market].byNullableObject("fundsProvider")
    and as an extension property
    val Capabilities.fundsProviderName: String get() = fundProvider?.get("name").nullString ?: ""
  • c

    carlbenson

    10/05/2017, 4:32 PM
    that passes my unit tests
  • m

    mkporwit

    11/26/2017, 7:17 AM
    I'm trying to deserialize a collection, and running into an issue with null. I have the following data class:
    Copy code
    data class DocumentMetadata(val documentId: String, val documentType: DocumentType = DocumentType.SKETCH, val owners: MutableSet<String> = mutableSetOf())
    and the following string representation:
    Copy code
    {
      "documentId": "__healthCheck",
      "documentType": "SKETCH"
    }
    I'm trying to just do
    val meta:DocumentMetadata = Gson().fromJson(metaString)
    , but owners comes up as
    null
    in the deserialized type, instead of the expected empty set of strings. Is there some setting to get the default data class values for any elements not in the input string, or will I have to do a
    TypeAdapter
    for this?
    f
    • 2
    • 3
  • x

    xxxifan

    03/22/2018, 5:48 AM
    Is this library inactive?
  • x

    xxxifan

    03/22/2018, 5:49 AM
    no more maintaining?
  • s

    salomonbrys

    03/28/2018, 10:29 AM
    Hi
  • s

    salomonbrys

    03/28/2018, 10:29 AM
    The library is widely used.
  • s

    salomonbrys

    03/28/2018, 10:30 AM
    I have been caught on other projects, but no, Kotson is not dead 😉
  • m

    MOZGIII

    04/09/2018, 1:13 AM
    Hey guys, how do I use it with ktor? I just want to get an exception when the non-nullable kotlin type is constructed. Instead I have nulls in non-nullable types.
  • m

    MOZGIII

    04/09/2018, 1:33 AM
    Still have issues with it. Is it suppose to work respect null-ability of the fields?
  • m

    mkporwit

    04/09/2018, 6:08 AM
    I don’ t think so 😕
  • m

    mkporwit

    04/09/2018, 6:08 AM
    I haven’t been able to get that to work.
  • m

    mkporwit

    04/09/2018, 6:09 AM
    I deserialize into data classes with non-nullable defaults yet I get null fields.
  • i

    Ian

    12/09/2018, 3:28 PM
    This is Kotson 2.3.0
  • c

    Caleb Hulbert

    08/18/2021, 5:27 PM
    Hi everyone, I've been running into an issue when using pure
    gson
    with kotlin, when using delegates. When deserializing and object. the delegate and the field it refers to are deserialized by reflection, and so they end up referring to different objects (i.e. using the delegate to `setValue`/`getValue` no longer updates the field it should be updating). I was wondering if
    Kotson
    has a was around this? The only way I have had luck so far was creating a custom deserializer for every class which uses delegates, but that's not really ideal.
    s
    • 2
    • 4