Logo: Scala Circe Json

Scala Circe Json

Organisatioun
cxcscmu
scala-circe-json

Mat der Circe-JSON-Bibliothéik a Scala fir typsécher JSON-Veraarbechtung schaffen

Add to TypingMind

Iwwersiicht

Editeurcxcscmu
RepositorySkillLearnBench
Skill-Nummscala-circe-json
Stären
91
Forks
7
Matgeliwwert Dateien
Nëmmen Instruktiounen
LizenzMIT
Linken
  • Instruktiounen am Markdown-Format

    Eng SKILL.md-Datei, déi de Modell bei Bedarf luet, soudatt nëmmen Token ufalen, wann eng Ufro tatsächlech dozou passt.

  • Funktionéiert mat all LLM

    KI-Skills si reng Markdown, keen ubidderspezifesche Code. Dofir funktionéiert dëse Skill mat GPT, Claude, Gemini, Grok oder engem lokale Modell.

  • Alles an den Instruktiounen

    Alles, wat de Modell brauch, steet an den Instruktiounen — keng zousätzlech Dateien ze synchroniséieren.

  • Open Source

    cxcscmu huet de Skill op GitHub verëffentlecht. Liest de Quelltext, ier Dir de Skill installéiert.

Installatioun

Installéiert de KI-Skill Scala Circe Json an TypingMind, fir e mat all LLM ze benotzen, oder setzt en an en anere KI-Agent an, deen SKILL.md liest.

1

An TypingMind installéieren

TypingMind installéiert e Skill direkt aus sengem GitHub-Dossier — dobäi gëtt SKILL.md gelies, d'Ressourcedateie gi gebëndelt an d'Resultat gëtt lokal gespäichert.

  1. Maacht d'App op a gitt op Plugins → Skills.
  2. Wielt „Vu GitHub installéieren“.
  3. Pecht d'URL vum Skill-Dossier, déi hei drënner steet, an a bestätegt d'Installatioun.
    https://github.com/cxcscmu/SkillLearnBench/tree/main/skills/b1-one-shot-claude-haiku-4-5/python-scala-translation/scala-circe-json
  4. Aktivéiert de Skill an all Chat, an deem Dir e benotze wëllt.
Plugins → Skills → Skill derbäisetzen → Iwwer eng GitHub-URL. Pecht dann d'URL vum Dossier an a klickt op Weider.
2

An engem anere KI-Agent installéieren

All KI-Agent, deen d'Format Agent Skills liest, kann dëse Skill benotzen — kopéiert den Dossier an d'Skill-Verzeechnes vun deem KI-Agent.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/cxcscmu/SkillLearnBench.git /tmp/SkillLearnBench
mkdir -p .claude/skills
cp -r /tmp/SkillLearnBench/skills/b1-one-shot-claude-haiku-4-5/python-scala-translation/scala-circe-json .claude/skills/scala-circe-json
Start Claude Code nom Kopéieren nei, fir datt den neie Skill erkannt gëtt.

An TypingMind benotzen

Aktivéiert Scala Circe Json an engem Chat vun TypingMind, an de Modell iwwerhëlt de Rescht. Den Numm an d'Beschreiwung stinn am System-Prompt. Soubal eng Ufro dozou passt, luet de Modell déi komplett Instruktioune selwer — Dir musst de Skill ni manuell opruffen, an et fale keng Token un, bis en tatsächlech benotzt gëtt.

De Modell luet Scala Circe Json automatesch, soubal eng Ufro dozou passt.

Funktionéiert mat all KI-Modell

KI-Skills si reng Markdown-Instruktiounen amplaz vun ubidderspezifeschem Code. Dofir ass Scala Circe Json net un de Modell gebonnen, fir deen de Skill geschriwwe gouf. Installéiert en eemol an TypingMind a benotzt e mat GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama oder engem lokale Modell, deen Dir selwer bedreift — alles mat Ären eegenen API-Schlësselen.

  • Gëtt nëmme gelueden, wann et néideg ass

    De System-Prompt enthält nëmmen den Numm an d'Beschreiwung. D'Instruktioune gi bei der éischter passender Ufro gelueden, soudatt en onbenotzte Skill näischt kascht.

  • De Modell am lafende Chat wiesselen

    Well de Skill aus Instruktiounen amplaz vu Code besteet, funktionéiert en och no engem Modellwiessel — den nächste Modell liest déi selwecht SKILL.md-Datei.

Skill-Instruktiounen

Dëst ass den Inhalt vu SKILL.md, deen de Modell luet. Liest e virun der Installatioun — e Skill besteet aus Instruktiounen, déi Äre Modell befollegt.

Scala Circe JSON Processing

Overview

Circe is the standard type-safe JSON library in Scala. It provides:

  • Immutable Json type
  • Type-safe codecs (Encoder/Decoder)
  • Cursor-based navigation (like XPath for JSON)
  • Pretty printing
  • Seamless case class integration

Installation

Add to build.sbt:

scala
libraryDependencies ++= Seq(
  "io.circe" %% "circe-core" % "0.14.5",
  "io.circe" %% "circe-parser" % "0.14.5",
  "io.circe" %% "circe-generic" % "0.14.5"
)

Basic Usage

Parsing JSON

scala
import io.circe.parser._
import io.circe._

// Parse from string
val jsonString = """{"name": "Alice", "age": 30}"""
val result: Either[io.circe.ParsingFailure, Json] = parse(jsonString)

// Safe extraction
result match {
  case Right(json) => println(s"Parsed: $json")
  case Left(error) => println(s"Parse error: $error")
}

// Or with Try
val json: scala.util.Try[Json] = parse(jsonString).toTry

Creating JSON

scala
import io.circe.syntax._

// From primitives
val str = Json.fromString("hello")
val num = Json.fromInt(42)
val bool = Json.fromBoolean(true)
val null_val = Json.Null

// Object (using syntax extension)
val obj = Json.obj(
  "name" -> Json.fromString("Alice"),
  "age" -> Json.fromInt(30)
)

// Array
val arr = Json.arr(
  Json.fromInt(1),
  Json.fromInt(2),
  Json.fromInt(3)
)

// Using tuple extension (cleaner)
val obj = Json.obj(
  "name" -> "Alice".asJson,
  "age" -> 30.asJson,
  "scores" -> List(90, 85, 88).asJson
)

Working with Json Type

scala
val json = Json.obj(
  "name" -> "Alice".asJson,
  "age" -> 30.asJson
)

// Type checks
json.isObject    // true
json.isArray     // false
json.isNull      // false

// Conversions
json.asObject    // Option[JsonObject]
json.asArray     // Option[Vector[Json]]
json.asString    // Option[String]
json.asNumber    // Option[JsonNumber]
json.asBoolean   // Option[Boolean]

// Unsafe conversion (throws)
json.asObject.get

Navigation with Cursors

Cursors provide safe, composable navigation (like XPath):

scala
import io.circe.{Json, HCursor}

val json = parse("""
{
  "person": {
    "name": "Alice",
    "addresses": [
      {"city": "NYC"},
      {"city": "Boston"}
    ]
  }
}
""").getOrElse(Json.Null)

// Creating cursor
val cursor: HCursor = json.hcursor

// Navigation
val name: Either[DecodingFailure, String] =
  cursor
    .downField("person")
    .downField("name")
    .as[String]

// Navigate arrays
val firstCity: Either[DecodingFailure, String] =
  cursor
    .downField("person")
    .downField("addresses")
    .downArray
    .downField("city")
    .as[String]

// Optional navigation (returns Json)
cursor.downField("person").downField("name").focus  // Option[Json]

Cursor Methods

scala
val cursor = json.hcursor

// Navigation
cursor.downField("fieldName")   // Navigate to field
cursor.downArray                // Navigate to first array element
cursor.up                       // Go to parent
cursor.left                     // Move to previous array element
cursor.right                    // Move to next array element

// Extraction
cursor.focus                    // Option[Json] - current value
cursor.as[String]               // Either[DecodingFailure, String]

// Checking
cursor.succeeded               // Boolean - did navigation succeed?

Type-Safe Encoding/Decoding

For Case Classes

scala
import io.circe._, io.circe.generic.semiauto._

case class Person(name: String, age: Int)

// Automatic codec generation
implicit val encoder: Encoder[Person] = deriveEncoder[Person]
implicit val decoder: Decoder[Person] = deriveDecoder[Person]

// Usage
val person = Person("Alice", 30)
val json = person.asJson
val decoded = json.as[Person]  // Either[DecodingFailure, Person]

Custom Codecs

scala
implicit val customEncoder: Encoder[Person] = new Encoder[Person] {
  def apply(p: Person): Json = Json.obj(
    "fullName" -> Json.fromString(p.name),
    "years" -> Json.fromInt(p.age)
  )
}

implicit val customDecoder: Decoder[Person] = new Decoder[Person] {
  def apply(c: HCursor): Decoder.Result[Person] = for {
    name <- c.downField("fullName").as[String]
    age <- c.downField("years").as[Int]
  } yield Person(name, age)
}

Recursive JSON Structures

For recursive types like JSON itself:

scala
// Representing JSON recursively
sealed trait JsonValue
case class JString(value: String) extends JsonValue
case class JNumber(value: Double) extends JsonValue
case class JBoolean(value: Boolean) extends JsonValue
case object JNull extends JsonValue
case class JArray(value: Vector[JsonValue]) extends JsonValue
case class JObject(value: Map[String, JsonValue]) extends JsonValue

// Or use Circe's Json type directly (which IS recursive)
val json: Json = Json.obj(
  "data" -> Json.arr(
    Json.obj("id" -> 1.asJson),
    Json.obj("id" -> 2.asJson)
  )
)

Pretty Printing

scala
import io.circe.Printer

val json = Json.obj("name" -> "Alice".asJson, "age" -> 30.asJson)

// Compact
json.toString()
// {"name":"Alice","age":30}

// Pretty printed
val printer = Printer.spaces2  // 2-space indent
println(printer.print(json))
// {
//   "name" : "Alice",
//   "age" : 30
// }

// Custom printer
val customPrinter = Printer(
  dropNullValues = false,
  indent = "  "
)

JSON Path Extraction (for tokenize_path)

scala
def getValueAtPath(json: Json, path: String): Option[Json] = {
  val parts = path.split("\\.")
  parts.foldLeft(Option(json)) { case (current, part) =>
    current.flatMap { j =>
      if (j.isObject) {
        j.hcursor.downField(part).focus
      } else if (j.isArray) {
        part.toIntOption.flatMap { idx =>
          j.asArray.flatMap(_.lift(idx))
        }
      } else {
        None
      }
    }
  }
}

// Usage
val json = parse("""{"person": {"name": "Alice", "scores": [90, 85]}}""").getOrElse(Json.Null)
val name = getValueAtPath(json, "person.name")      // Some("Alice")
val score = getValueAtPath(json, "person.scores.0") // Some(90)
val missing = getValueAtPath(json, "person.email")  // None

Common Patterns for Tokenizer

JSON Tokenization

scala
import io.circe.Json
import io.circe.syntax._

class JsonTokenizer(pretty: Boolean = false) {
  def tokenize(json: Json): Token = {
    val jsonStr = if (pretty) {
      Printer.spaces2.print(json)
    } else {
      json.toString()
    }
    Token(jsonStr, TokenType.STRUCTURED, Map("json" -> true.asJson))
  }

  def tokenizePath(json: Json, path: String): Option[Token] = {
    getValueAtPath(json, path).map(tokenize)
  }

  private def getValueAtPath(json: Json, path: String): Option[Json] = {
    val parts = path.split("\\.")
    parts.foldLeft(Option(json)) { case (current, part) =>
      current.flatMap { j =>
        if (j.isObject) j.hcursor.downField(part).focus
        else if (j.isArray) {
          part.toIntOption.flatMap(idx => j.asArray.flatMap(_.lift(idx)))
        } else None
      }
    }
  }
}

Type Safety Benefits

Unlike Python's duck typing with JSON:

python
# Python - runtime errors possible
data = {"name": "Alice"}
age = data["age"]  # KeyError!
scala
// Scala - compile-time safety
val json = parse("""{"name": "Alice"}""").getOrElse(Json.Null)
val age: Option[Int] = json.hcursor.downField("age").as[Int].toOption
// Type mismatch caught at compile time

Circe vs Manual JSON Handling

scala
// Manual approach (error-prone)
val jsonStr = s"""{"value": "$value", "type": "$tokenType"}"""

// Circe approach (safe, composable)
val token = Token(value, tokenType)
val json = token.asJson  // With derived encoders

Heefeg gestallte Froen

Wat mécht de KI-Skill Scala Circe Json?

Mat der Circe-JSON-Bibliothéik a Scala fir typsécher JSON-Veraarbechtung schaffen

Firwat Scala Circe Json op TypingMind benotzen?

Well Dir en eemol installéiert a mat all Modell benotze kënnt. Scala Circe Json besteet aus einfachem Markdown amplaz vun ubidderspezifeschem Code. Dee selwechte Skill funktionéiert also mat GPT-5, Claude, Gemini, Grok oder engem lokale Modell – an Dir kënnt de Modell matzen am Chat wiesselen, ouni datt de Skill ophält ze funktionéieren. TypingMind benotzt Är eegen API-Schlësselen, sou datt Dir d'Ubidder direkt bezuelt, amplaz en Abonnement pro Benotzerplaz ze bezuelen. Är Skills a Chats bleiwen an Ärem eegene Späicher.

Wéi installéieren ech Scala Circe Json an TypingMind?

Maacht an TypingMind Plugins → Skills → Vu GitHub installéieren op a setzt https://github.com/cxcscmu/SkillLearnBench/tree/main/skills/b1-one-shot-claude-haiku-4-5/python-scala-translation/scala-circe-json an. TypingMind liest d'Datei SKILL.md an installéiert de Skill, deen Dir pro Chat aktivéiere kënnt.

Wéi eng KI-Modeller kënnen Scala Circe Json benotzen?

All Modell, deen Dir an TypingMind verbënnt. KI-Skills sinn einfach Markdown-Instruktiounen an net ubidderspezifesche Code. Dofir kënne GPT, Claude, Gemini, Grok a lokal Modeller dëse Skill lueden, wann eng Ufro dozou passt.

Wéi vill KI-Modeller kann ech mat Scala Circe Json benotzen?

Esou vill wéi Dir wëllt. Soulaang e Modell Skills ënnerstëtzt, kënnt Dir Scala Circe Json domat benotzen – zum Beispill mat GPT, Claude, Gemini, Grok, DeepSeek, Mistral oder Llama – alles op TypingMind mat Ären eegenen API-Schlësselen.

Ass de KI-Skill Scala Circe Json gratis?

Jo. cxcscmu huet de Skill ënner der Lizenz MIT op GitHub publizéiert. Dir bezuelt nëmmen Ären eegene KI-Ubidder fir d'Token, déi Dir benotzt.

Wat si KI-Skills?

E KI-Skill ass e weiderverwendbare Pak vun Instruktiounen, deen engem KI-Modell bäibréngt, wéi en eng bestëmmt Aufgab erfëllt. E follegt dem oppenen Agent Skills-Format: eng Datei SKILL.md mat engem Numm an enger Beschreiwung, plus all Scripten, Virlagen oder Referenzdateien, déi de Modell eventuell brauch. De Modell liest d'Instruktiounen eréischt, wann Är Ufro zum Skill passt. En installéierte Skill kascht also näischt, bis e benotzt gëtt.

Wéi ënnerscheede sech KI-Skills vu Plugins oder MCP-Serveren?

E Plugin oder MCP-Server gëtt engem Modell nei Tools, déi en opruffe kann – Code, deen iergendwou leeft an e Resultat zeréckgëtt. E KI-Skill gëtt dem Modell dogéint Wëssen an eng Virgoensweis: wéi en eng Aufgab ugeet, wéi eng Schrëtt e maache soll a wéi eng gutt Ausgab ausgesäit. Skills bestinn aus einfachem Markdown, brauche kee Server, keen API-Schlëssel a kee Lafzäitëmfeld a funktionéiere mat all Modell.

Alleguer weisen

Elo Ären eegene KI-Aarbechtsberäich ariichten

Abonnéiert Iech op eisen Newsletter, fir iwwer nei Funktiounen an zukünfteg Gewënnspiller informéiert ze ginn 👇