<commit_before>package se.marcuslonnberg.scaladocker.remote.models.json

import org.joda.time.format.ISODateTimeFormat
import org.json4s.JsonAST.JObject
import org.json4s.JsonDSL._
import org.json4s._
import se.marcuslonnberg.scaladocker.remote.models.JsonFormatHelpers._
import se.marcuslonnberg.scaladocker.remote.models._

object ContainerStateSerializer extends CustomSerializer[ContainerState]({ implicit formats =>
  val DateExtractor = """(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3})\d+Z""".r
  val DateFormat = ISODateTimeFormat.dateTime
  def extractDateTimeField(fieldName: String)(implicit obj: JObject) = {
    extractFieldOpt[String](fieldName).filter(_ != "0001-01-01T00:00:00Z").map {
      case DateExtractor(isoFormat) =>
        DateFormat.parseDateTime(isoFormat + "Z")
    }
  }

  ({
    case obj: JObject =>
      implicit val o = obj

      val running = extractField[Boolean]("Running")
      val paused = extractField[Boolean]("Paused")
      val restarting = extractField[Boolean]("Restarting")
      val pid = extractField[Int]("Pid")
      val exitCode = extractField[Int]("ExitCode")

      val startedAt = extractDateTimeField("StartedAt")
      val finishedAt = extractDateTimeField("FinishedAt")

      ContainerState(
        running = running,
        paused = paused,
        restarting = restarting,
        pid = pid,
        exitCode = exitCode,
        startedAt = startedAt,
        finishedAt = finishedAt)
  }, {
    case state: ContainerState =>
      ("Running" -> state.running) ~
        ("Paused" -> state.paused) ~
        ("Restarting" -> state.restarting) ~
        ("Pid" -> state.pid) ~<commit_msg>Fix container state date deserializer<commit_after>package se.marcuslonnberg.scaladocker.remote.models.json

import org.joda.time.DateTime
import org.json4s.JsonAST.JObject
import org.json4s.JsonDSL._
import org.json4s._
import se.marcuslonnberg.scaladocker.remote.models.JsonFormatHelpers._
import se.marcuslonnberg.scaladocker.remote.models._

object ContainerStateSerializer extends CustomSerializer[ContainerState]({ implicit formats =>
  def extractDateTimeField(fieldName: String)(implicit obj: JObject): Option[DateTime] = {
    obj \ fieldName match {
      case JString("0001-01-01T00:00:00Z") => None
      case _ =>
        extractFieldOpt[DateTime](fieldName)
    }
  }

  ({
    case obj: JObject =>
      implicit val o = obj

      val running = extractField[Boolean]("Running")
      val paused = extractField[Boolean]("Paused")
      val restarting = extractField[Boolean]("Restarting")
      val pid = extractField[Int]("Pid")
      val exitCode = extractField[Int]("ExitCode")

      val startedAt = extractDateTimeField("StartedAt")
      val finishedAt = extractDateTimeField("FinishedAt")

      ContainerState(
        running = running,
        paused = paused,
        restarting = restarting,
        pid = pid,
        exitCode = exitCode,
        startedAt = startedAt,
        finishedAt = finishedAt)
  }, {
    case state: ContainerState =>
      ("Running" -> state.running) ~
        ("Paused" -> state.paused) ~
        ("Restarting" -> state.restarting) ~
        ("Pid" -> state.pid) ~
{'fraction_non_alphanumeric': 0.12271293375394322, 'fraction_numerical': 0.012933753943217666, 'mean_word_length': 3.3497942386831276, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '2503671', 'n_tokens_mistral': 995, 'n_tokens_neox': 971, 'n_words': 183}