This is an automated email from the ASF dual-hosted git repository.

aradzinski pushed a commit to branch NLPCRAFT-108
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git


The following commit(s) were added to refs/heads/NLPCRAFT-108 by this push:
     new 7641396  WIP.
7641396 is described below

commit 764139606ff70f50a05c61cf9a9927ef3468deca
Author: Aaron Radzinski <[email protected]>
AuthorDate: Wed Oct 7 01:34:29 2020 -0700

    WIP.
---
 .../org/apache/nlpcraft/common/util/NCUtils.scala  | 36 ++++++++++++++++------
 .../nlpcraft/model/tools/cmdline/NCCli.scala       |  9 ++++--
 2 files changed, 32 insertions(+), 13 deletions(-)

diff --git 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/util/NCUtils.scala 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/util/NCUtils.scala
index 34675f3..55eb51b 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/util/NCUtils.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/util/NCUtils.scala
@@ -39,7 +39,7 @@ import com.fasterxml.jackson.core.`type`.TypeReference
 import com.fasterxml.jackson.databind.{DeserializationFeature, ObjectMapper}
 import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
 import com.fasterxml.jackson.module.scala.DefaultScalaModule
-import com.google.gson.Gson
+import com.google.gson.{GsonBuilder, JsonElement}
 import com.typesafe.scalalogging.{LazyLogging, Logger}
 import org.apache.commons.codec.binary.Base64
 import org.apache.commons.codec.digest.DigestUtils
@@ -60,7 +60,7 @@ import scala.language.{implicitConversions, postfixOps}
 import scala.reflect.runtime.universe._
 import scala.sys.SystemProperties
 import scala.util.control.Exception.ignoring
-import scala.util.{Failure, Success}
+import scala.util._
 
 /**
   * Project-wide, global utilities ans miscellaneous functions.
@@ -104,14 +104,12 @@ object NCUtils extends LazyLogging {
 
     private final lazy val DEC_FMT_SYMS = new DecimalFormatSymbols(Locale.US)
 
-    private final lazy val GSON = new Gson()
-    private final lazy val YAML = {
-        new ObjectMapper(new YAMLFactory).
-            configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, 
false).
-            registerModule(new DefaultScalaModule()).
-            setSerializationInclusion(Include.NON_NULL).
-            setSerializationInclusion(Include.NON_EMPTY)
-    }
+    private final lazy val GSON = new 
GsonBuilder().setPrettyPrinting().create()
+    private final lazy val YAML = new ObjectMapper(new YAMLFactory).
+        configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false).
+        registerModule(new DefaultScalaModule()).
+        setSerializationInclusion(Include.NON_NULL).
+        setSerializationInclusion(Include.NON_EMPTY)
 
     private def mkDecimalFormat(ptrn: String) = {
         val df = new DecimalFormat(ptrn, DEC_FMT_SYMS)
@@ -1421,11 +1419,29 @@ object NCUtils extends LazyLogging {
             }
         }
 
+        buf.append(RST)
+
         buf.toString()
     }
 
     /**
      *
+     * @param jsStr
+     * @return
+     */
+    def prettyJson(jsStr: String): String =
+        GSON.toJson(GSON.getAdapter(classOf[JsonElement]).fromJson(jsStr))
+
+    /**
+     *
+     * @param jsStr
+     * @return
+     */
+    def isJsonValid(jsStr: String): Boolean =
+        
scala.util.Try(GSON.getAdapter(classOf[JsonElement]).fromJson(jsStr)).isSuccess
+
+    /**
+     *
      * @param namePrefix
      * @param threadNum
      * @return
diff --git 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCli.scala 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCli.scala
index ff05ec6..720ffa7 100644
--- 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCli.scala
+++ 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCli.scala
@@ -140,6 +140,8 @@ object NCCli extends App {
         )
     case class HttpError(httpCode: Int)
         extends IllegalStateException(s"REST error (HTTP ${c(httpCode)}).")
+    case class MalformedJson()
+        extends IllegalStateException("Malformed JSON.")
     case class TooManyArguments(cmd: Command)
         extends IllegalArgumentException(s"Too many arguments, type $C'help 
--cmd=${cmd.name}'$RST to get help.")
     case class NotEnoughArguments(cmd: Command)
@@ -1333,9 +1335,10 @@ object NCCli extends App {
      */
     private def cmdRest(cmd: Command, args: Seq[Argument], repl: Boolean): 
Unit = {
         val path = args.find(_.parameter.id == "path").getOrElse(throw 
MissingParameter(cmd, "path")).value.get
-        val rawJson = args.find(_.parameter.id == "json").getOrElse(throw 
MissingParameter(cmd, "json")).value.get
+        val rawJson = stripQuotes(args.find(_.parameter.id == 
"json").getOrElse(throw MissingParameter(cmd, "json")).value.get)
 
-        // TODO: verify JSON
+        if (!U.isJsonValid(rawJson))
+            throw MalformedJson()
 
         if (!REST_PATHS.exists(_._1 == path))
             throw new IllegalArgumentException(s"Unknown REST path 
$C'$path'$RST, type ${c("'help --cmd=rest'")} to get help.")
@@ -1370,7 +1373,7 @@ object NCCli extends App {
         }
 
         if (resp.code == 200 || resp.code == 400)
-            logln(resp.data) // TODO
+            logln(U.colorJson(U.prettyJson(resp.data))) // TODO
         else
             logln(resp.data)
     }

Reply via email to