python-scala-circe-json
Using the circe library for JSON encoding, decoding, and manipulation in Scala as a replacement for Python's json module, covering the Json ADT, parsing, and path traversal.
- License 许可
- MIT
- 最近更新
- 2026-10-02
- Stars
- ★ 93
- Forks
- 8
01 前言 Front Matter
- name
- python-scala-circe-json
- description
- Using the circe library for JSON encoding, decoding, and manipulation in Scala as a replacement for Python's json module, covering the Json ADT, parsing, and path traversal.
03 SKILL.md 正文
Circe JSON in Scala
Setup (build.sbt)
libraryDependencies ++= Seq(
"io.circe" %% "circe-core" % "0.14.x",
"io.circe" %% "circe-parser" % "0.14.x",
"io.circe" %% "circe-generic" % "0.14.x"
)
Imports
import io.circe.{Json, JsonObject}
import io.circe.parser._
import io.circe.syntax._
Python json Module → Circe
Parsing
import json
data = json.loads('{"key": "value"}')
import io.circe.parser._
val result: Either[io.circe.ParsingFailure, Json] = parse("""{"key": "value"}""")
val json: Json = parse("""{"key": "value"}""").getOrElse(Json.Null)
Serializing
json.dumps(value) # compact
json.dumps(value, indent=2) # pretty
json.noSpaces // compact
json.spaces2 // pretty-printed (2-space indent)
json.spaces4 // pretty-printed (4-space indent)
Circe Json ADT
Json.Null // null
Json.True / Json.False // booleans
Json.fromString("hi") // string
Json.fromInt(42) // number
Json.fromDouble(3.14) // number
Json.arr(j1, j2) // array
Json.obj("k" -> v) // object
Accessing Values (Safe Traversal)
val json: Json = ...
// Object field
json.asObject.flatMap(_.apply("key")) // Option[Json]
// Array element
json.asArray.flatMap(_.lift(0)) // Option[Json]
// Type extraction
json.asString // Option[String]
json.asNumber // Option[JsonNumber]
json.asBoolean // Option[Boolean]
json.asArray // Option[Vector[Json]]
json.asObject // Option[JsonObject]
Path Traversal (Recursive)
def traversePath(json: Json, path: String): Option[Json] = {
path.split("\\.").foldLeft(Option(json)) { (current, part) =>
current.flatMap { j =>
j.asObject.flatMap(_.apply(part))
.orElse(
j.asArray.flatMap { arr =>
scala.util.Try(part.toInt).toOption
.flatMap(idx => if (idx >= 0 && idx < arr.length) Some(arr(idx)) else None)
}
)
}
}
}
Replacing Python json.dumps with Circe
json.dumps(value) # → value.noSpaces
json.dumps(value, indent=2) # → value.spaces2
Metadata with Boolean Values
Since Map[String, Any] is used for metadata, Boolean fits naturally:
Map("json" -> true) // Map[String, Any] — Boolean is subtype of Any
04 同仓库更多 Skills
object_counter
Count occurrences of an object in the image using computer vision algorithm.
travel-planning-workflow
Invoke this skill when performing travel planning. It defines a structured workflow for generating itineraries. Following this workflow is essential to ensure the final plan satisfies user requirements.
bundled-dataset-search-tool
invoke this skill when you need to perform database search for travel planning. This skill provides some useful pre-packaged tools to look up accommodations, attractions, cities, driving distance, flights, and restaurants from the bundled dataset.
glm-output
Output-processing guidance for GLM tasks. Especially useful after glm-basics and glm-calibration when you need verifier-matching metrics from output.nc and a final /root/metrics.json.
glm-calibration
Calibration guidance for GLM tasks. Often most effective after glm-basics has clarified the setup; glm-output is the companion skill for exact final metric computation.
glm-basics
A good starting point for GLM calibration tasks. Use to inspect glm3.nml, confirm how GLM runs, and identify the relevant files before moving on to calibration and output evaluation.