<commit_before>/**********************************************************************************************************************
 * Copyright © 2014 Reactific Software LLC                                                                            *
 *                                                                                                                    *
 * This file is part of Scrupal, an Opinionated Web Application Framework.                                            *
 *                                                                                                                    *
 * Scrupal is free software: you can redistribute it and/or modify it under the terms                                 *
 * of the GNU General Public License as published by the Free Software Foundation,                                    *
 * either version 3 of the License, or (at your option) any later version.                                            *
 *                                                                                                                    *
 * Scrupal is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;                               *
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                          *
 * See the GNU General Public License for more details.                                                               *
 *                                                                                                                    *
 * You should have received a copy of the GNU General Public License along with Scrupal.                              *
 * If not, see either: http://www.gnu.org/licenses or http://opensource.org/licenses/GPL-3.0.                         *
 **********************************************************************************************************************/

package scrupal.db

import reactivemongo.bson._
import reactivemongo.bson.utils.Converters
import play.api.libs.json._
import play.api.libs.json.Json.toJsFieldJsValueWrapper
import scala.math.BigDecimal.double2bigDecimal
import scala.math.BigDecimal.int2bigDecimal
import scala.math.BigDecimal.long2bigDecimal
import scala.util.{Failure, Success, Try}

/** Conversion Between JSON and BSON
  *
  * This object provides the JSONReads and JSONWrites objects needed to handle conversion between JSON format and BSON
  * format. The JSON Format objects come to us from the play-json library, with thanks!
  */
object SONConversion {

  def toBSON(json: JsValue): JsResult[BSONValue] = {
      StringFormat.pReads.
      orElse(ArrayFormat.pReads).
      orElse(ObjectIDFormat.pReads).
      orElse(DateTimeFormat.pReads).
      orElse(BinaryFormat.pReads).
      orElse(BooleanFormat.pReads).
      orElse(DoubleFormat.pReads).
      orElse(LongFormat.pReads).
      orElse(IntegerFormat.pReads).
      orElse(DBPointerFormat.pReads).
      orElse(RegexFormat.pReads).
      orElse(TimestampFormat.pReads).
      orElse(JavaScriptFormat.pReads).
      orElse(JavaScriptWSFormat.pReads).
      orElse(DocumentFormat.pReads).
      orElse(SymbolFormat.pReads).
      orElse(UndefinedFormat.pReads).
      orElse(NullFormat.pReads).
      lift(json).getOrElse(
          JsError(s"JsValue unrecognized: $json")
        )
  }

  def toJSON(bson: BSONValue): JsValue = {
    bson.code match {
      case 3 ⇒ DocumentFormat.writes(bson.asInstanceOf[BSONDocument])
      case 2 ⇒ StringFormat.writes(bson.asInstanceOf[BSONString])
      case 4 ⇒ ArrayFormat.writes(bson.asInstanceOf[BSONArray])
      case 7 ⇒ ObjectIDFormat.writes(bson.asInstanceOf[BSONObjectID])
      case 9 ⇒ DateTimeFormat.writes(bson.asInstanceOf[BSONDateTime])
      case 12 ⇒ DBPointerFormat.writes(bson.asInstanceOf[BSONDBPointer])
      case 8 ⇒ BooleanFormat.writes(bson.asInstanceOf[BSONBoolean])
      case 18 ⇒ LongFormat.writes(bson.asInstanceOf[BSONLong])
      case 16 ⇒ IntegerFormat.writes(bson.asInstanceOf[BSONInteger])
      case 1 ⇒ DoubleFormat.writes(bson.asInstanceOf[BSONDouble])
      case 5 ⇒ BinaryFormat.writes(bson.asInstanceOf[BSONBinary])
      case 11 ⇒ RegexFormat.writes(bson.asInstanceOf[BSONRegex])
      case 14 ⇒ SymbolFormat.writes(bson.asInstanceOf[BSONSymbol])
      case 6 ⇒ UndefinedFormat.writes(bson.asInstanceOf[BSONUndefined.type])
      case 10 ⇒ NullFormat.writes(bson.asInstanceOf[BSONNull.type])
      case 13 ⇒ JavaScriptFormat.writes(bson.asInstanceOf[BSONJavaScript])
      case 15 ⇒ JavaScriptWSFormat.writes(bson.asInstanceOf[BSONJavaScriptWS])
      case 17 ⇒ TimestampFormat.writes(bson.asInstanceOf[BSONTimestamp])
      case _ ⇒ throw new Exception(s"BSONValue unregocnized: $bson")
    }<commit_msg>Fix a typo - InProgress #2 * Make an error message readable.<commit_after>/**********************************************************************************************************************
 * Copyright © 2014 Reactific Software LLC                                                                            *
 *                                                                                                                    *
 * This file is part of Scrupal, an Opinionated Web Application Framework.                                            *
 *                                                                                                                    *
 * Scrupal is free software: you can redistribute it and/or modify it under the terms                                 *
 * of the GNU General Public License as published by the Free Software Foundation,                                    *
 * either version 3 of the License, or (at your option) any later version.                                            *
 *                                                                                                                    *
 * Scrupal is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;                               *
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                          *
 * See the GNU General Public License for more details.                                                               *
 *                                                                                                                    *
 * You should have received a copy of the GNU General Public License along with Scrupal.                              *
 * If not, see either: http://www.gnu.org/licenses or http://opensource.org/licenses/GPL-3.0.                         *
 **********************************************************************************************************************/

package scrupal.db

import reactivemongo.bson._
import reactivemongo.bson.utils.Converters
import play.api.libs.json._
import play.api.libs.json.Json.toJsFieldJsValueWrapper
import scala.math.BigDecimal.double2bigDecimal
import scala.math.BigDecimal.int2bigDecimal
import scala.math.BigDecimal.long2bigDecimal
import scala.util.{Failure, Success, Try}

/** Conversion Between JSON and BSON
  *
  * This object provides the JSONReads and JSONWrites objects needed to handle conversion between JSON format and BSON
  * format. The JSON Format objects come to us from the play-json library, with thanks!
  */
object SONConversion {

  def toBSON(json: JsValue): JsResult[BSONValue] = {
      StringFormat.pReads.
      orElse(ArrayFormat.pReads).
      orElse(ObjectIDFormat.pReads).
      orElse(DateTimeFormat.pReads).
      orElse(BinaryFormat.pReads).
      orElse(BooleanFormat.pReads).
      orElse(DoubleFormat.pReads).
      orElse(LongFormat.pReads).
      orElse(IntegerFormat.pReads).
      orElse(DBPointerFormat.pReads).
      orElse(RegexFormat.pReads).
      orElse(TimestampFormat.pReads).
      orElse(JavaScriptFormat.pReads).
      orElse(JavaScriptWSFormat.pReads).
      orElse(DocumentFormat.pReads).
      orElse(SymbolFormat.pReads).
      orElse(UndefinedFormat.pReads).
      orElse(NullFormat.pReads).
      lift(json).getOrElse(
          JsError(s"JsValue unrecognized: $json")
        )
  }

  def toJSON(bson: BSONValue): JsValue = {
    bson.code match {
      case 3 ⇒ DocumentFormat.writes(bson.asInstanceOf[BSONDocument])
      case 2 ⇒ StringFormat.writes(bson.asInstanceOf[BSONString])
      case 4 ⇒ ArrayFormat.writes(bson.asInstanceOf[BSONArray])
      case 7 ⇒ ObjectIDFormat.writes(bson.asInstanceOf[BSONObjectID])
      case 9 ⇒ DateTimeFormat.writes(bson.asInstanceOf[BSONDateTime])
      case 12 ⇒ DBPointerFormat.writes(bson.asInstanceOf[BSONDBPointer])
      case 8 ⇒ BooleanFormat.writes(bson.asInstanceOf[BSONBoolean])
      case 18 ⇒ LongFormat.writes(bson.asInstanceOf[BSONLong])
      case 16 ⇒ IntegerFormat.writes(bson.asInstanceOf[BSONInteger])
      case 1 ⇒ DoubleFormat.writes(bson.asInstanceOf[BSONDouble])
      case 5 ⇒ BinaryFormat.writes(bson.asInstanceOf[BSONBinary])
      case 11 ⇒ RegexFormat.writes(bson.asInstanceOf[BSONRegex])
      case 14 ⇒ SymbolFormat.writes(bson.asInstanceOf[BSONSymbol])
      case 6 ⇒ UndefinedFormat.writes(bson.asInstanceOf[BSONUndefined.type])
      case 10 ⇒ NullFormat.writes(bson.asInstanceOf[BSONNull.type])
      case 13 ⇒ JavaScriptFormat.writes(bson.asInstanceOf[BSONJavaScript])
      case 15 ⇒ JavaScriptWSFormat.writes(bson.asInstanceOf[BSONJavaScriptWS])
      case 17 ⇒ TimestampFormat.writes(bson.asInstanceOf[BSONTimestamp])
      case _ ⇒ throw new Exception(s"BSONValue unrecognized: $bson")
    }
{'fraction_non_alphanumeric': 0.121351605816508, 'fraction_numerical': 0.007846008996756982, 'mean_word_length': 1.9707893101305158, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 60, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5705106', 'n_tokens_mistral': 2506, 'n_tokens_neox': 2408, 'n_words': 581}