<commit_before>package uk.ac.wellcome.models

import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.{
  DeserializationContext,
  JsonDeserializer,
  JsonNode
}
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
import com.twitter.inject.Logging

class IdentifierSchemeDeserialiser
    extends JsonDeserializer[IdentifierSchemes.IdentifierScheme]
    with Logging {

  override def deserialize(
    p: JsonParser,
    ctxt: DeserializationContext): IdentifierSchemes.IdentifierScheme = {
    val node: JsonNode = p.getCodec.readTree(p)
    val identifierScheme = node.get("identifierScheme").asText
    createIdentifierScheme(identifierScheme)
  }

  private def createIdentifierScheme(
    identifierScheme: String): IdentifierSchemes.IdentifierScheme = {
    identifierScheme match {
      case s: String if s == IdentifierSchemes.miroImageNumber.toString =>
        IdentifierSchemes.miroImageNumber
      case s: String if s == IdentifierSchemes.sierraSystemNumber.toString =>
        IdentifierSchemes.sierraSystemNumber
      case s: String if s == IdentifierSchemes.calmAltRefNo.toString =>
        IdentifierSchemes.calmAltRefNo
      case s: String if s == IdentifierSchemes.calmPlaceholder.toString =>
        IdentifierSchemes.calmPlaceholder
      case s: String if s == IdentifierSchemes.miroLibraryReference.toString =>
        IdentifierSchemes.miroLibraryReference
      case identifierScheme =>
        val errorMessage = s"$identifierScheme is not a valid identifierScheme"
        error(errorMessage)
        throw new Exception(errorMessage)
    }
  }
}

/** This is the canonical version of our identifier schemes.  This contains
  *  the strings that will be presented to users of the API.
  */
object IdentifierSchemes {
  @JsonDeserialize(using = classOf[IdentifierSchemeDeserialiser])
  sealed trait IdentifierScheme
  // Corresponds to the image number in Miro, e.g. V00127563.
  case object miroImageNumber extends IdentifierScheme {
    override def toString: String = "miro-image-number"
  }

  // Corresponds to legacy image IDs from the Miro data.  Eventually these
  // should be tackled properly: either assigned proper identifier schemes
  // and normalised, or removed entirely.  For now, this is a stopgap to
  // get these IDs into the API until we deal with them properly.
  case object miroLibraryReference extends IdentifierScheme {
    override def toString: String = "miro-library-reference"
  }

  // Placeholder until we ingest real Calm records.
  // TODO: Replace this with something more appropriate
  case object calmPlaceholder extends IdentifierScheme {
    override def toString: String = "calm-placeholder"
  }

  case object calmAltRefNo extends IdentifierScheme {
    override def toString: String = "calm-altrefno"
  }
<commit_msg>Fix serialisation and deserialisation<commit_after>package uk.ac.wellcome.models

import com.fasterxml.jackson.core.{JsonGenerator, JsonParser}
import com.fasterxml.jackson.databind._
import com.fasterxml.jackson.databind.annotation.{JsonDeserialize, JsonSerialize}
import com.twitter.inject.Logging

class IdentifierSchemeDeserialiser
    extends JsonDeserializer[IdentifierSchemes.IdentifierScheme]
    with Logging {

  override def deserialize(
    p: JsonParser,
    ctxt: DeserializationContext): IdentifierSchemes.IdentifierScheme = {
    val node: JsonNode = p.getCodec.readTree(p)
    val identifierScheme = node.asText()
    createIdentifierScheme(identifierScheme)
  }

  private def createIdentifierScheme(
    identifierScheme: String): IdentifierSchemes.IdentifierScheme = {
    identifierScheme match {
      case s: String if s == IdentifierSchemes.miroImageNumber.toString =>
        IdentifierSchemes.miroImageNumber
      case s: String if s == IdentifierSchemes.sierraSystemNumber.toString =>
        IdentifierSchemes.sierraSystemNumber
      case s: String if s == IdentifierSchemes.calmAltRefNo.toString =>
        IdentifierSchemes.calmAltRefNo
      case s: String if s == IdentifierSchemes.calmPlaceholder.toString =>
        IdentifierSchemes.calmPlaceholder
      case s: String if s == IdentifierSchemes.miroLibraryReference.toString =>
        IdentifierSchemes.miroLibraryReference
      case identifierScheme =>
        val errorMessage = s"$identifierScheme is not a valid identifierScheme"
        error(errorMessage)
        throw new Exception(errorMessage)
    }
  }
}

class IdentifierSchemeSerialiser extends JsonSerializer[IdentifierSchemes.IdentifierScheme] {
  override def serialize(value: IdentifierSchemes.IdentifierScheme, gen: JsonGenerator, serializers: SerializerProvider): Unit = {
    gen.writeString(value.toString)
  }
}

/** This is the canonical version of our identifier schemes.  This contains
  *  the strings that will be presented to users of the API.
  */
object IdentifierSchemes {
  @JsonDeserialize(using = classOf[IdentifierSchemeDeserialiser])
  @JsonSerialize(using = classOf[IdentifierSchemeSerialiser])
  sealed trait IdentifierScheme
  // Corresponds to the image number in Miro, e.g. V00127563.
  case object miroImageNumber extends IdentifierScheme {
    override def toString: String = "miro-image-number"
  }

  // Corresponds to legacy image IDs from the Miro data.  Eventually these
  // should be tackled properly: either assigned proper identifier schemes
  // and normalised, or removed entirely.  For now, this is a stopgap to
  // get these IDs into the API until we deal with them properly.
  case object miroLibraryReference extends IdentifierScheme {
    override def toString: String = "miro-library-reference"
  }

  // Placeholder until we ingest real Calm records.
  // TODO: Replace this with something more appropriate
  case object calmPlaceholder extends IdentifierScheme {
    override def toString: String = "calm-placeholder"
  }

  case object calmAltRefNo extends IdentifierScheme {
    override def toString: String = "calm-altrefno"
  }

{'fraction_non_alphanumeric': 0.06705783738474434, 'fraction_numerical': 0.002682313495389774, 'mean_word_length': 4.099145299145299, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '678967', 'n_tokens_mistral': 1620, 'n_tokens_neox': 1567, 'n_words': 474}