<commit_before>package pl.touk.nussknacker.engine.util.typing

import com.typesafe.scalalogging.LazyLogging
import io.circe.{Json, JsonObject}
import org.everit.json.schema.{ArraySchema, BooleanSchema, FalseSchema, NullSchema, NumberSchema, ObjectSchema, Schema, StringSchema, TrueSchema}
import pl.touk.nussknacker.engine.api.typed.TypedMap
import pl.touk.nussknacker.engine.api.typed.typing.{Typed, TypedObjectTypingResult, TypingResult}

import scala.collection.JavaConverters._

object JsonToTypedMapConverter {

  def jsonToTypedMap(json: Json): TypedMap =
    jsonObjectToTypedMap(json.asObject.getOrElse(JsonObject.empty))

  private def jsonToAny(json: Json): Any = {
    json.fold(
      jsonNull = null,
      jsonBoolean = identity,
      jsonNumber = d => d.toBigDecimal.map(_.bigDecimal).getOrElse(d.toDouble),
      jsonString = identity,
      jsonArray = _.map(f => jsonToAny(f)).asJava,
      jsonObject = jsonObjectToTypedMap
    )
  }

  private def jsonObjectToTypedMap(jsonObject: JsonObject): TypedMap = {
    val res = jsonObject.toMap.map {
      case (jsonFieldName, json) => jsonFieldName -> jsonToAny(json)
    }
    TypedMap(res)
  }
}
<commit_msg>fix circe toBigDecimal looses precision<commit_after>package pl.touk.nussknacker.engine.util.typing

import com.typesafe.scalalogging.LazyLogging
import io.circe.{Json, JsonObject}
import org.everit.json.schema.{ArraySchema, BooleanSchema, FalseSchema, NullSchema, NumberSchema, ObjectSchema, Schema, StringSchema, TrueSchema}
import pl.touk.nussknacker.engine.api.typed.TypedMap
import pl.touk.nussknacker.engine.api.typed.typing.{Typed, TypedObjectTypingResult, TypingResult}

import scala.collection.JavaConverters._

object JsonToTypedMapConverter {

  def jsonToTypedMap(json: Json): TypedMap =
    jsonObjectToTypedMap(json.asObject.getOrElse(JsonObject.empty))

  private def jsonToAny(json: Json): Any = {
    json.fold(
      jsonNull = null,
      jsonBoolean = identity,
      jsonNumber = d => {
        //circe 0.14.1 still looses precision as was in https://github.com/circe/circe/issues/1101.
        //d.toBigDecimal.map(_.bigDecimal).getOrElse(d.toDouble)
        new java.math.BigDecimal(d.toString)
      },
      jsonString = identity,
      jsonArray = _.map(f => jsonToAny(f)).asJava,
      jsonObject = jsonObjectToTypedMap
    )
  }

  private def jsonObjectToTypedMap(jsonObject: JsonObject): TypedMap = {
    val res = jsonObject.toMap.map {
      case (jsonFieldName, json) => jsonFieldName -> jsonToAny(json)
    }
    TypedMap(res)
  }
}

{'fraction_non_alphanumeric': 0.10853322847031066, 'fraction_numerical': 0.0031458906802988595, 'mean_word_length': 4.13939393939394, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 1, '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': '4049804', 'n_tokens_mistral': 866, 'n_tokens_neox': 850, 'n_words': 147}