Commit aeab1cba authored by Tim Stadtmüller's avatar Tim Stadtmüller Committed by Jaco Hofmann
Browse files

Added Functionallity to parse full Json Objects to Features

parent a04c817a
......@@ -174,27 +174,85 @@ package object json {
(JsPath \ "AverageClockCycles").readNullable[Int]
) (Core.apply _)
implicit val coreWrites: Writes[Core] = (
(JsPath \ "DescPath").write[Path].transform((js: JsObject) => js - "DescPath") ~
(JsPath \ "ZipFile").write[Path] ~
(JsPath \ "Name").write[String] ~
(JsPath \ "Id").write[Int] ~
(JsPath \ "Version").write[String] ~
(JsPath \ "Target").write[TargetDesc] ~
(JsPath \ "Description").writeNullable[String] ~
(JsPath \ "AverageClockCycles").writeNullable[Int]
) (unlift(Core.unapply _))
(JsPath \ "DescPath").write[Path].transform((js: JsObject) => js - "DescPath") ~
(JsPath \ "ZipFile").write[Path] ~
(JsPath \ "Name").write[String] ~
(JsPath \ "Id").write[Int] ~
(JsPath \ "Version").write[String] ~
(JsPath \ "Target").write[TargetDesc] ~
(JsPath \ "Description").writeNullable[String] ~
(JsPath \ "AverageClockCycles").writeNullable[Int]
) (unlift(Core.unapply _))
/* Core @} */
/* @{ Features */
implicit val readsFeature: Reads[Feature] = (
(JsPath \ "Feature").read[String] ~
(JsPath \ "Properties").read[Map[String, String]]
(JsPath \ "Properties").read[Feature.FMap]
) (Feature.apply _)
implicit lazy val readsFeatureMap: Reads[Feature.FMap] = new Reads[Feature.FMap]{
def reads(json: JsValue): JsResult[Feature.FMap] = {
val temp = json.validate[Map[String, Feature.FValue]]
val result: JsResult[Feature.FMap] = temp.asEither match {
case Right(s) => new JsSuccess[Feature.FMap](Feature.FMap(s.toMap))
case Left(e) => new JsError(e)
}
result
}
}
implicit val readsFeatureList: Reads[Feature.FList] = new Reads[Feature.FList]{
def reads(json: JsValue): JsResult[Feature.FList] = {
val temp = json.validate[Seq[Feature.FValue]]
val result: JsResult[Feature.FList] = temp.asEither match {
case Right(s) => new JsSuccess[Feature.FList](Feature.FList(s.toList))
case Left(e) => new JsError(e)
}
result
}
}
implicit val readsFeatureString: Reads[Feature.FString] = new Reads[Feature.FString]{
def reads(json: JsValue): JsResult[Feature.FString] ={
val temp = json match {
case s: JsString => s.validate[String]
case i: JsNumber => i.validate[Double]
case b: JsBoolean => b.validate[Boolean]
// case n: JsNull => new JsSuccess[String]("null")
}
val result = temp.asEither match{
case Right(s) => new JsSuccess[Feature.FString](Feature.FString(s"${s}"))
case Left(e) => new JsError(e)
}
result
}
}
implicit val readsFeatureValue: Reads[Feature.FValue] = new Reads[Feature.FValue]{
def reads(json: JsValue): JsResult[Feature.FValue] = {
val temp: JsResult[Feature.FValue] = json match {
case m: JsObject => m.validate[Feature.FMap]
case l: JsArray => l.validate[Feature.FList]
case x: JsValue => x.validate[Feature.FString]
}
temp
}
}
implicit val writesFeature: Writes[Feature] = (
(JsPath \ "Feature").write[String] ~
(JsPath \ "Properties").write[Map[String, String]]
(JsPath \ "Properties").write[Feature.FMap]
) (unlift(Feature.unapply _))
implicit lazy val writesFeatureMap: Writes[Feature.FMap] = new Writes[Feature.FMap]{
def writes(myMap: Feature.FMap) = Json.parse(myMap.toJson)
}
/* Features @} */
/* @{ Kernel.Argument */
......
......@@ -19,15 +19,20 @@
package de.tu_darmstadt.cs.esa.tapasco.parser
import de.tu_darmstadt.cs.esa.tapasco.base.Feature
import fastparse.all._
import java.nio.file._
import scala.io.Source
private object FeatureParsers {
import BasicParsers._
def feature: Parser[Feature] =
(qstring.opaque("feature name").! ~ ws ~/
featureBegin ~ ws ~/
(featureKeyValue ~ ws).rep ~ ws ~/
featureEnd ~ ws)
.map(p => Feature(p._1, p._2.toMap))
(fconfigFile | fprops)).map(p => Feature(p._1, p._2))
val fprops: Parser[Feature.FMap] =
fMap
def fconfigFile:Parser[Feature.FMap] =
"configFile" ~ ws ~ featureAssign ~/ path.opaque("path to .json file with Feature") map loadFeatureFromFile _
def features: Parser[(String, Seq[Feature])] =
longOption("features", "Features") ~ ws ~/ seqOne(feature)
......@@ -43,16 +48,40 @@ private object FeatureParsers {
CharIn(featureEndChars).opaque(s"end of feature mark, one of '$featureEndChars'")
def featureAssign: Parser[Unit] = "->" | "=" | ":=" | ":"
def featureKey: Parser[String] =
val featureKey: Parser[String] =
(quotedString | string(featureAssigns ++ featureMarks))
.opaque("feature key name")
def featureVal: Parser[String] =
(quotedString | string(featureAssigns ++ featureMarks))
.opaque("feature value for given key")
val featureVal: Parser[Feature.FValue] =
(fString | fMap | fList)
.opaque("feature value for given key (May be a simple value, an object or a List of Objects)")
def featureKeyValue: Parser[(String, String)] =
val featureKeyValue: Parser[(String, Feature.FValue)] =
featureKey ~ ws ~/
featureAssign.opaque("feature assignment operator, one of '->', '=', ':=' or ':'") ~ ws ~/
featureVal ~ ws
val fString: Parser[Feature.FString] =
(quotedString | string(featureAssigns ++ featureMarks))
.map (s =>Feature.FString(s))
val fList:Parser[Feature.FList] =
("[" ~ ws ~/
(featureVal ~ sep).rep ~ ws ~/
"]" ~ ws)
.map(p => Feature.FList(p.toList))
val fMap: Parser[Feature.FMap] =
("{" ~ ws ~/
(featureKeyValue ~ ws).rep ~ ws ~/
"}" ~ ws)
.map(p => Feature.FMap(p.toMap))
def loadFeatureFromFile(from: Path): Feature.FMap = {
CommandLineParser.check(fprops.parse(Source.fromFile(from.toString).getLines.mkString)) match {
case Right(s) => s
case Left(e) => throw e
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment