All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.http4k.connect.openai.action.KotshiEmbeddingJsonAdapter.kt Maven / Gradle / Ivy

// Code generated by Kotshi. Do not edit.
package org.http4k.connect.openai.action

import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.JsonDataException
import com.squareup.moshi.JsonReader
import com.squareup.moshi.JsonWriter
import com.squareup.moshi.Moshi
import java.io.IOException
import java.lang.StringBuilder
import kotlin.Boolean
import kotlin.FloatArray
import kotlin.Int
import kotlin.Suppress
import kotlin.apply
import kotlin.collections.setOf
import kotlin.jvm.Throws
import se.ansman.kotshi.InternalKotshiApi
import se.ansman.kotshi.KotshiUtils.appendNullableError
import se.ansman.kotshi.NamedJsonAdapter

@InternalKotshiApi
@Suppress(
  "DEPRECATION",
  "unused",
  "ClassName",
  "REDUNDANT_PROJECTION",
  "RedundantExplicitType",
  "LocalVariableName",
  "RedundantVisibilityModifier",
  "PLATFORM_CLASS_MAPPED_TO_KOTLIN",
  "IMPLICIT_NOTHING_TYPE_ARGUMENT_IN_RETURN_POSITION",
  "EXPERIMENTAL_API_USAGE",
  "OPT_IN_USAGE",
)
internal class KotshiEmbeddingJsonAdapter(
  moshi: Moshi,
) : NamedJsonAdapter("KotshiJsonAdapter(Embedding)") {
  private val embeddingAdapter: JsonAdapter = moshi.adapter(
      FloatArray::class.javaObjectType,
      setOf(),
      "embedding"
  )

  private val options: JsonReader.Options = JsonReader.Options.of(
      "embedding",
      "index"
  )

  @Throws(IOException::class)
  override fun toJson(writer: JsonWriter, `value`: Embedding?) {
    if (`value` == null) {
      writer.nullValue()
      return
    }
    writer
      .beginObject()
      .name("embedding").apply {
        embeddingAdapter.toJson(this, `value`.embedding)
      }
      .name("index").value(`value`.index)
      .endObject()
  }

  @Throws(IOException::class)
  override fun fromJson(reader: JsonReader): Embedding? {
    if (reader.peek() == JsonReader.Token.NULL) return reader.nextNull()

    var embedding: FloatArray? = null
    var index: Int = 0
    var indexIsSet: Boolean = false

    reader.beginObject()
    while (reader.hasNext()) {
      when (reader.selectName(options)) {
        0 -> {
          embedding = embeddingAdapter.fromJson(reader)
        }
        1 -> {
          if (reader.peek() == JsonReader.Token.NULL) {
            reader.skipValue()
          } else {
            index = reader.nextInt()
            indexIsSet = true
          }
        }
        -1 -> {
          reader.skipName()
          reader.skipValue()
        }
      }
    }
    reader.endObject()

    var errorBuilder: StringBuilder? = null
    if (embedding == null) {
      errorBuilder = errorBuilder.appendNullableError("embedding")
    }
    if (!indexIsSet) {
      errorBuilder = errorBuilder.appendNullableError("index")
    }
    if (errorBuilder != null) {
      errorBuilder.append(" (at path ").append(reader.path).append(')')
      throw JsonDataException(errorBuilder.toString())
    }

    return Embedding(
        embedding = embedding!!,
        index = index
    )
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy