src/Pure/General/json.scala
author wenzelm
Wed, 11 Nov 2020 21:04:22 +0100
changeset 72575 c7ab83a0c564
parent 71776 5ef7f374e0f8
child 73340 0ffcad1f6130
permissions -rw-r--r--
tuned signature;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
63992
3aa9837d05c7 updated headers;
wenzelm
parents: 63644
diff changeset
     1
/*  Title:      Pure/General/json.scala
63644
ed266398da33 support for JSON parsing;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
ed266398da33 support for JSON parsing;
wenzelm
parents:
diff changeset
     3
67819
b73d8ed73b35 tuned comments;
wenzelm
parents: 67736
diff changeset
     4
Support for JSON: https://www.json.org/.
67874
599753dd6501 more comments;
wenzelm
parents: 67864
diff changeset
     5
599753dd6501 more comments;
wenzelm
parents: 67864
diff changeset
     6
See also http://seriot.ch/parsing_json.php "Parsing JSON is a Minefield".
63644
ed266398da33 support for JSON parsing;
wenzelm
parents:
diff changeset
     7
*/
ed266398da33 support for JSON parsing;
wenzelm
parents:
diff changeset
     8
ed266398da33 support for JSON parsing;
wenzelm
parents:
diff changeset
     9
package isabelle
ed266398da33 support for JSON parsing;
wenzelm
parents:
diff changeset
    10
ed266398da33 support for JSON parsing;
wenzelm
parents:
diff changeset
    11
71601
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 71305
diff changeset
    12
import scala.util.matching.Regex
66925
8b117dc73278 separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents: 66924
diff changeset
    13
import scala.util.parsing.combinator.Parsers
66922
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
    14
import scala.util.parsing.combinator.lexical.Scanners
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
    15
import scala.util.parsing.input.CharArrayReader.EofCh
64545
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
    16
66922
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
    17
63644
ed266398da33 support for JSON parsing;
wenzelm
parents:
diff changeset
    18
object JSON
ed266398da33 support for JSON parsing;
wenzelm
parents:
diff changeset
    19
{
64545
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
    20
  type T = Any
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
    21
  type S = String
63644
ed266398da33 support for JSON parsing;
wenzelm
parents:
diff changeset
    22
67841
8ada8f6d9495 clarified signature;
wenzelm
parents: 67819
diff changeset
    23
  object Object
8ada8f6d9495 clarified signature;
wenzelm
parents: 67819
diff changeset
    24
  {
67857
262d62a4c32b more informative error with JSON result;
wenzelm
parents: 67850
diff changeset
    25
    type Entry = (String, JSON.T)
262d62a4c32b more informative error with JSON result;
wenzelm
parents: 67850
diff changeset
    26
67841
8ada8f6d9495 clarified signature;
wenzelm
parents: 67819
diff changeset
    27
    type T = Map[String, JSON.T]
67850
3e9fe7a84b5d tuned signature;
wenzelm
parents: 67843
diff changeset
    28
    val empty: Object.T = Map.empty
3e9fe7a84b5d tuned signature;
wenzelm
parents: 67843
diff changeset
    29
67857
262d62a4c32b more informative error with JSON result;
wenzelm
parents: 67850
diff changeset
    30
    def apply(entries: Entry*): Object.T = Map(entries:_*)
67841
8ada8f6d9495 clarified signature;
wenzelm
parents: 67819
diff changeset
    31
71776
5ef7f374e0f8 clarified signature -- avoid warning;
wenzelm
parents: 71601
diff changeset
    32
    def unapply(obj: Any): Option[Object.T] =
67841
8ada8f6d9495 clarified signature;
wenzelm
parents: 67819
diff changeset
    33
      obj match {
8ada8f6d9495 clarified signature;
wenzelm
parents: 67819
diff changeset
    34
        case m: Map[_, _] if m.keySet.forall(_.isInstanceOf[String]) =>
8ada8f6d9495 clarified signature;
wenzelm
parents: 67819
diff changeset
    35
          Some(m.asInstanceOf[Object.T])
8ada8f6d9495 clarified signature;
wenzelm
parents: 67819
diff changeset
    36
        case _ => None
8ada8f6d9495 clarified signature;
wenzelm
parents: 67819
diff changeset
    37
      }
8ada8f6d9495 clarified signature;
wenzelm
parents: 67819
diff changeset
    38
  }
66928
33f9133bed7c tuned signature;
wenzelm
parents: 66926
diff changeset
    39
63644
ed266398da33 support for JSON parsing;
wenzelm
parents:
diff changeset
    40
66922
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
    41
  /* lexer */
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
    42
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
    43
  object Kind extends Enumeration
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
    44
  {
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
    45
    val KEYWORD, STRING, NUMBER, ERROR = Value
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
    46
  }
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
    47
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
    48
  sealed case class Token(kind: Kind.Value, text: String)
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
    49
  {
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
    50
    def is_keyword: Boolean = kind == Kind.KEYWORD
66925
8b117dc73278 separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents: 66924
diff changeset
    51
    def is_keyword(name: String): Boolean = kind == Kind.KEYWORD && text == name
66922
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
    52
    def is_string: Boolean = kind == Kind.STRING
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
    53
    def is_number: Boolean = kind == Kind.NUMBER
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
    54
    def is_error: Boolean = kind == Kind.ERROR
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
    55
  }
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
    56
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
    57
  object Lexer extends Scanners with Scan.Parsers
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
    58
  {
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
    59
    override type Elem = Char
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
    60
    type Token = JSON.Token
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
    61
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
    62
    def errorToken(msg: String): Token = Token(Kind.ERROR, msg)
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
    63
67111
42f290d8ccbd more accurate JSON parsing according to http://seriot.ch/parsing_json.php
wenzelm
parents: 66928
diff changeset
    64
    val white_space: String = " \t\n\r"
71601
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 71305
diff changeset
    65
    override val whiteSpace: Regex = ("[" + white_space + "]+").r
67111
42f290d8ccbd more accurate JSON parsing according to http://seriot.ch/parsing_json.php
wenzelm
parents: 66928
diff changeset
    66
    def whitespace: Parser[Any] = many(character(white_space.contains(_)))
66922
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
    67
71601
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 71305
diff changeset
    68
    val letter: Parser[String] = one(character(Symbol.is_ascii_letter))
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 71305
diff changeset
    69
    val letters1: Parser[String] = many1(character(Symbol.is_ascii_letter))
66922
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
    70
71601
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 71305
diff changeset
    71
    def digits: Parser[String] = many(character(Symbol.is_ascii_digit))
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 71305
diff changeset
    72
    def digits1: Parser[String] = many1(character(Symbol.is_ascii_digit))
66922
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
    73
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
    74
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
    75
    /* keyword */
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
    76
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
    77
    def keyword: Parser[Token] =
66924
b4d4027f743b more permissive;
wenzelm
parents: 66922
diff changeset
    78
      (letters1 | one(character("{}[],:".contains(_)))) ^^ (s => Token(Kind.KEYWORD, s))
66922
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
    79
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
    80
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
    81
    /* string */
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
    82
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
    83
    def string: Parser[Token] =
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
    84
      '\"' ~> rep(string_body) <~ '\"' ^^ (cs => Token(Kind.STRING, cs.mkString))
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
    85
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
    86
    def string_body: Parser[Char] =
67111
42f290d8ccbd more accurate JSON parsing according to http://seriot.ch/parsing_json.php
wenzelm
parents: 66928
diff changeset
    87
      elem("", c => c > '\u001f' && c != '\"' && c != '\\' && c != EofCh) | '\\' ~> string_escape
66922
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
    88
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
    89
    def string_escape: Parser[Char] =
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
    90
      elem("", "\"\\/".contains(_)) |
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
    91
      elem("", "bfnrt".contains(_)) ^^
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
    92
        { case 'b' => '\b' case 'f' => '\f' case 'n' => '\n' case 'r' => '\r' case 't' => '\t' } |
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
    93
      'u' ~> repeated(character("0123456789abcdefABCDEF".contains(_)), 4, 4) ^^
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
    94
        (s => Integer.parseInt(s, 16).toChar)
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
    95
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
    96
    def string_failure: Parser[Token] = '\"' ~> failure("Unterminated string")
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
    97
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
    98
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
    99
    /* number */
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
   100
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
   101
    def number: Parser[Token] =
67111
42f290d8ccbd more accurate JSON parsing according to http://seriot.ch/parsing_json.php
wenzelm
parents: 66928
diff changeset
   102
      opt("-") ~ number_body ~ opt(letter) ^^ {
66922
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
   103
        case a ~ b ~ None => Token(Kind.NUMBER, a.getOrElse("") + b)
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
   104
        case a ~ b ~ Some(c) =>
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
   105
          errorToken("Invalid number format: " + quote(a.getOrElse("") + b + c))
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
   106
      }
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
   107
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
   108
    def number_body: Parser[String] =
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
   109
      (zero | positive) ~ opt(number_fract) ~ opt(number_exp) ^^
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
   110
        { case a ~ b ~ c => a + b.getOrElse("") + c.getOrElse("") }
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
   111
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
   112
    def number_fract: Parser[String] = "." ~ digits1 ^^ { case a ~ b => a + b }
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
   113
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
   114
    def number_exp: Parser[String] =
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
   115
      one(character("eE".contains(_))) ~ maybe(character("-+".contains(_))) ~ digits1 ^^
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
   116
        { case a ~ b ~ c => a + b + c }
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
   117
71601
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 71305
diff changeset
   118
    def zero: Parser[String] = one(character(c => c == '0'))
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 71305
diff changeset
   119
    def nonzero: Parser[String] = one(character(c => c != '0' && Symbol.is_ascii_digit(c)))
66922
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
   120
    def positive: Parser[String] = nonzero ~ digits ^^ { case a ~ b => a + b }
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
   121
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
   122
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
   123
    /* token */
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
   124
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
   125
    def token: Parser[Token] =
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
   126
      keyword | (string | (string_failure | (number | failure("Illegal character"))))
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
   127
  }
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
   128
5a476a87a535 separate JSON lexer;
wenzelm
parents: 64878
diff changeset
   129
66925
8b117dc73278 separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents: 66924
diff changeset
   130
  /* parser */
8b117dc73278 separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents: 66924
diff changeset
   131
8b117dc73278 separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents: 66924
diff changeset
   132
  trait Parser extends Parsers
8b117dc73278 separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents: 66924
diff changeset
   133
  {
8b117dc73278 separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents: 66924
diff changeset
   134
    type Elem = Token
8b117dc73278 separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents: 66924
diff changeset
   135
8b117dc73278 separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents: 66924
diff changeset
   136
    def $$$(name: String): Parser[Token] = elem(name, _.is_keyword(name))
8b117dc73278 separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents: 66924
diff changeset
   137
    def string: Parser[String] = elem("string", _.is_string) ^^ (_.text)
8b117dc73278 separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents: 66924
diff changeset
   138
    def number: Parser[Double] = elem("number", _.is_number) ^^ (tok => tok.text.toDouble)
8b117dc73278 separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents: 66924
diff changeset
   139
67841
8ada8f6d9495 clarified signature;
wenzelm
parents: 67819
diff changeset
   140
    def json_object: Parser[Object.T] =
66925
8b117dc73278 separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents: 66924
diff changeset
   141
      $$$("{") ~>
8b117dc73278 separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents: 66924
diff changeset
   142
        repsep(string ~ ($$$(":") ~> json_value) ^^ { case a ~ b => (a, b) }, $$$(",")) <~
8b117dc73278 separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents: 66924
diff changeset
   143
      $$$("}") ^^ (_.toMap)
8b117dc73278 separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents: 66924
diff changeset
   144
8b117dc73278 separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents: 66924
diff changeset
   145
    def json_array: Parser[List[T]] =
8b117dc73278 separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents: 66924
diff changeset
   146
      $$$("[") ~> repsep(json_value, $$$(",")) <~ $$$("]")
8b117dc73278 separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents: 66924
diff changeset
   147
8b117dc73278 separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents: 66924
diff changeset
   148
    def json_value: Parser[T] =
8b117dc73278 separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents: 66924
diff changeset
   149
      json_object | (json_array | (number | (string |
8b117dc73278 separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents: 66924
diff changeset
   150
        ($$$("true") ^^^ true | ($$$("false") ^^^ false | ($$$("null") ^^^ null))))))
8b117dc73278 separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents: 66924
diff changeset
   151
8b117dc73278 separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents: 66924
diff changeset
   152
    def parse(input: CharSequence, strict: Boolean): T =
8b117dc73278 separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents: 66924
diff changeset
   153
    {
8b117dc73278 separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents: 66924
diff changeset
   154
      val scanner = new Lexer.Scanner(Scan.char_reader(input))
8b117dc73278 separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents: 66924
diff changeset
   155
      phrase(if (strict) json_object | json_array else json_value)(scanner) match {
8b117dc73278 separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents: 66924
diff changeset
   156
        case Success(json, _) => json
8b117dc73278 separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents: 66924
diff changeset
   157
        case NoSuccess(_, next) => error("Malformed JSON input at " + next.pos)
8b117dc73278 separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents: 66924
diff changeset
   158
      }
8b117dc73278 separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents: 66924
diff changeset
   159
    }
8b117dc73278 separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents: 66924
diff changeset
   160
  }
8b117dc73278 separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents: 66924
diff changeset
   161
8b117dc73278 separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents: 66924
diff changeset
   162
  object Parser extends Parser
8b117dc73278 separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents: 66924
diff changeset
   163
8b117dc73278 separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents: 66924
diff changeset
   164
64545
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   165
  /* standard format */
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   166
66925
8b117dc73278 separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents: 66924
diff changeset
   167
  def parse(s: S, strict: Boolean = true): T = Parser.parse(s, strict)
64545
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   168
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   169
  object Format
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   170
  {
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   171
    def unapply(s: S): Option[T] =
66925
8b117dc73278 separate JSON parser, which is slightly more general than (deprecated) scala.util.parsing.json.JSON;
wenzelm
parents: 66924
diff changeset
   172
      try { Some(parse(s, strict = false)) }
64545
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   173
      catch { case ERROR(_) => None }
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   174
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   175
    def apply(json: T): S =
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   176
    {
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   177
      val result = new StringBuilder
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   178
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   179
      def string(s: String)
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   180
      {
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   181
        result += '"'
64761
ae97a5afffcc avoid escape of '/' in JSONFormat.quoteString;
wenzelm
parents: 64545
diff changeset
   182
        result ++=
ae97a5afffcc avoid escape of '/' in JSONFormat.quoteString;
wenzelm
parents: 64545
diff changeset
   183
          s.iterator.map {
ae97a5afffcc avoid escape of '/' in JSONFormat.quoteString;
wenzelm
parents: 64545
diff changeset
   184
            case '"'  => "\\\""
ae97a5afffcc avoid escape of '/' in JSONFormat.quoteString;
wenzelm
parents: 64545
diff changeset
   185
            case '\\' => "\\\\"
ae97a5afffcc avoid escape of '/' in JSONFormat.quoteString;
wenzelm
parents: 64545
diff changeset
   186
            case '\b' => "\\b"
ae97a5afffcc avoid escape of '/' in JSONFormat.quoteString;
wenzelm
parents: 64545
diff changeset
   187
            case '\f' => "\\f"
ae97a5afffcc avoid escape of '/' in JSONFormat.quoteString;
wenzelm
parents: 64545
diff changeset
   188
            case '\n' => "\\n"
ae97a5afffcc avoid escape of '/' in JSONFormat.quoteString;
wenzelm
parents: 64545
diff changeset
   189
            case '\r' => "\\r"
ae97a5afffcc avoid escape of '/' in JSONFormat.quoteString;
wenzelm
parents: 64545
diff changeset
   190
            case '\t' => "\\t"
ae97a5afffcc avoid escape of '/' in JSONFormat.quoteString;
wenzelm
parents: 64545
diff changeset
   191
            case c =>
ae97a5afffcc avoid escape of '/' in JSONFormat.quoteString;
wenzelm
parents: 64545
diff changeset
   192
              if (c <= '\u001f' || c >= '\u007f' && c <= '\u009f') "\\u%04x".format(c.toInt)
ae97a5afffcc avoid escape of '/' in JSONFormat.quoteString;
wenzelm
parents: 64545
diff changeset
   193
              else c
ae97a5afffcc avoid escape of '/' in JSONFormat.quoteString;
wenzelm
parents: 64545
diff changeset
   194
          }.mkString
64545
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   195
        result += '"'
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   196
      }
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   197
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   198
      def array(list: List[T])
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   199
      {
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   200
        result += '['
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   201
        Library.separate(None, list.map(Some(_))).foreach({
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   202
          case None => result += ','
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   203
          case Some(x) => json_format(x)
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   204
        })
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   205
        result += ']'
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   206
      }
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   207
67841
8ada8f6d9495 clarified signature;
wenzelm
parents: 67819
diff changeset
   208
      def object_(obj: Object.T)
64545
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   209
      {
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   210
        result += '{'
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   211
        Library.separate(None, obj.toList.map(Some(_))).foreach({
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   212
          case None => result += ','
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   213
          case Some((x, y)) =>
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   214
            string(x)
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   215
            result += ':'
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   216
            json_format(y)
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   217
        })
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   218
        result += '}'
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   219
      }
63644
ed266398da33 support for JSON parsing;
wenzelm
parents:
diff changeset
   220
64545
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   221
      def json_format(x: T)
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   222
      {
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   223
        x match {
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   224
          case null => result ++= "null"
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   225
          case _: Int | _: Long | _: Boolean => result ++= x.toString
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   226
          case n: Double =>
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   227
            val i = n.toLong
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   228
            result ++= (if (i.toDouble == n) i.toString else n.toString)
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   229
          case s: String => string(s)
67841
8ada8f6d9495 clarified signature;
wenzelm
parents: 67819
diff changeset
   230
          case Object(m) => object_(m)
64545
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   231
          case list: List[T] => array(list)
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   232
          case _ => error("Bad JSON value: " + x.toString)
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   233
        }
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   234
      }
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   235
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   236
      json_format(json)
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   237
      result.toString
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   238
    }
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   239
  }
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   240
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   241
67843
ff561f6e0a8e more operations for typed JSON values;
wenzelm
parents: 67842
diff changeset
   242
  /* typed values */
64545
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   243
67842
ff87225e7e8e clarified signature;
wenzelm
parents: 67841
diff changeset
   244
  object Value
64545
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   245
  {
67885
839a624aabb9 prefer typed UUID;
wenzelm
parents: 67874
diff changeset
   246
    object UUID
839a624aabb9 prefer typed UUID;
wenzelm
parents: 67874
diff changeset
   247
    {
69458
5655af3ea5bd clarified modules and signature;
wenzelm
parents: 68943
diff changeset
   248
      def unapply(json: T): Option[isabelle.UUID.T] =
67885
839a624aabb9 prefer typed UUID;
wenzelm
parents: 67874
diff changeset
   249
        json match {
69458
5655af3ea5bd clarified modules and signature;
wenzelm
parents: 68943
diff changeset
   250
          case x: java.lang.String => isabelle.UUID.unapply(x)
67885
839a624aabb9 prefer typed UUID;
wenzelm
parents: 67874
diff changeset
   251
          case _ => None
839a624aabb9 prefer typed UUID;
wenzelm
parents: 67874
diff changeset
   252
        }
839a624aabb9 prefer typed UUID;
wenzelm
parents: 67874
diff changeset
   253
    }
839a624aabb9 prefer typed UUID;
wenzelm
parents: 67874
diff changeset
   254
71298
93b097726667 tuned whitespace;
wenzelm
parents: 69458
diff changeset
   255
    object String
93b097726667 tuned whitespace;
wenzelm
parents: 69458
diff changeset
   256
    {
67842
ff87225e7e8e clarified signature;
wenzelm
parents: 67841
diff changeset
   257
      def unapply(json: T): Option[java.lang.String] =
ff87225e7e8e clarified signature;
wenzelm
parents: 67841
diff changeset
   258
        json match {
ff87225e7e8e clarified signature;
wenzelm
parents: 67841
diff changeset
   259
          case x: java.lang.String => Some(x)
ff87225e7e8e clarified signature;
wenzelm
parents: 67841
diff changeset
   260
          case _ => None
ff87225e7e8e clarified signature;
wenzelm
parents: 67841
diff changeset
   261
        }
ff87225e7e8e clarified signature;
wenzelm
parents: 67841
diff changeset
   262
    }
ff87225e7e8e clarified signature;
wenzelm
parents: 67841
diff changeset
   263
71305
2f7da37bab52 more permissive string fields;
wenzelm
parents: 71298
diff changeset
   264
    object String0
2f7da37bab52 more permissive string fields;
wenzelm
parents: 71298
diff changeset
   265
    {
2f7da37bab52 more permissive string fields;
wenzelm
parents: 71298
diff changeset
   266
      def unapply(json: T): Option[java.lang.String] =
2f7da37bab52 more permissive string fields;
wenzelm
parents: 71298
diff changeset
   267
        json match {
2f7da37bab52 more permissive string fields;
wenzelm
parents: 71298
diff changeset
   268
          case null => Some("")
2f7da37bab52 more permissive string fields;
wenzelm
parents: 71298
diff changeset
   269
          case x: java.lang.String => Some(x)
2f7da37bab52 more permissive string fields;
wenzelm
parents: 71298
diff changeset
   270
          case _ => None
2f7da37bab52 more permissive string fields;
wenzelm
parents: 71298
diff changeset
   271
        }
2f7da37bab52 more permissive string fields;
wenzelm
parents: 71298
diff changeset
   272
    }
2f7da37bab52 more permissive string fields;
wenzelm
parents: 71298
diff changeset
   273
71298
93b097726667 tuned whitespace;
wenzelm
parents: 69458
diff changeset
   274
    object Double
93b097726667 tuned whitespace;
wenzelm
parents: 69458
diff changeset
   275
    {
64545
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   276
      def unapply(json: T): Option[scala.Double] =
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   277
        json match {
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   278
          case x: scala.Double => Some(x)
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   279
          case x: scala.Long => Some(x.toDouble)
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   280
          case x: scala.Int => Some(x.toDouble)
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   281
          case _ => None
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   282
        }
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   283
    }
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   284
71298
93b097726667 tuned whitespace;
wenzelm
parents: 69458
diff changeset
   285
    object Long
93b097726667 tuned whitespace;
wenzelm
parents: 69458
diff changeset
   286
    {
64545
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   287
      def unapply(json: T): Option[scala.Long] =
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   288
        json match {
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   289
          case x: scala.Double if x.toLong.toDouble == x => Some(x.toLong)
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   290
          case x: scala.Long => Some(x)
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   291
          case x: scala.Int => Some(x.toLong)
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   292
          case _ => None
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   293
        }
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   294
    }
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   295
71298
93b097726667 tuned whitespace;
wenzelm
parents: 69458
diff changeset
   296
    object Int
93b097726667 tuned whitespace;
wenzelm
parents: 69458
diff changeset
   297
    {
64545
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   298
      def unapply(json: T): Option[scala.Int] =
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   299
        json match {
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   300
          case x: scala.Double if x.toInt.toDouble == x => Some(x.toInt)
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   301
          case x: scala.Long if x.toInt.toLong == x => Some(x.toInt)
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   302
          case x: scala.Int => Some(x)
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   303
          case _ => None
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   304
        }
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   305
    }
67842
ff87225e7e8e clarified signature;
wenzelm
parents: 67841
diff changeset
   306
71298
93b097726667 tuned whitespace;
wenzelm
parents: 69458
diff changeset
   307
    object Boolean
93b097726667 tuned whitespace;
wenzelm
parents: 69458
diff changeset
   308
    {
67842
ff87225e7e8e clarified signature;
wenzelm
parents: 67841
diff changeset
   309
      def unapply(json: T): Option[scala.Boolean] =
ff87225e7e8e clarified signature;
wenzelm
parents: 67841
diff changeset
   310
        json match {
ff87225e7e8e clarified signature;
wenzelm
parents: 67841
diff changeset
   311
          case x: scala.Boolean => Some(x)
ff87225e7e8e clarified signature;
wenzelm
parents: 67841
diff changeset
   312
          case _ => None
ff87225e7e8e clarified signature;
wenzelm
parents: 67841
diff changeset
   313
        }
ff87225e7e8e clarified signature;
wenzelm
parents: 67841
diff changeset
   314
    }
67843
ff561f6e0a8e more operations for typed JSON values;
wenzelm
parents: 67842
diff changeset
   315
ff561f6e0a8e more operations for typed JSON values;
wenzelm
parents: 67842
diff changeset
   316
    object List
ff561f6e0a8e more operations for typed JSON values;
wenzelm
parents: 67842
diff changeset
   317
    {
ff561f6e0a8e more operations for typed JSON values;
wenzelm
parents: 67842
diff changeset
   318
      def unapply[A](json: T, unapply: T => Option[A]): Option[List[A]] =
ff561f6e0a8e more operations for typed JSON values;
wenzelm
parents: 67842
diff changeset
   319
        json match {
ff561f6e0a8e more operations for typed JSON values;
wenzelm
parents: 67842
diff changeset
   320
          case xs: List[T] =>
ff561f6e0a8e more operations for typed JSON values;
wenzelm
parents: 67842
diff changeset
   321
            val ys = xs.map(unapply)
ff561f6e0a8e more operations for typed JSON values;
wenzelm
parents: 67842
diff changeset
   322
            if (ys.forall(_.isDefined)) Some(ys.map(_.get)) else None
ff561f6e0a8e more operations for typed JSON values;
wenzelm
parents: 67842
diff changeset
   323
          case _ => None
ff561f6e0a8e more operations for typed JSON values;
wenzelm
parents: 67842
diff changeset
   324
        }
ff561f6e0a8e more operations for typed JSON values;
wenzelm
parents: 67842
diff changeset
   325
    }
68943
e564605d4cac tuned signature;
wenzelm
parents: 68742
diff changeset
   326
71298
93b097726667 tuned whitespace;
wenzelm
parents: 69458
diff changeset
   327
    object Seconds
93b097726667 tuned whitespace;
wenzelm
parents: 69458
diff changeset
   328
    {
68943
e564605d4cac tuned signature;
wenzelm
parents: 68742
diff changeset
   329
      def unapply(json: T): Option[Time] =
71601
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 71305
diff changeset
   330
        Double.unapply(json).map(Time.seconds)
68943
e564605d4cac tuned signature;
wenzelm
parents: 68742
diff changeset
   331
    }
64545
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   332
  }
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   333
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   334
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   335
  /* object values */
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   336
67841
8ada8f6d9495 clarified signature;
wenzelm
parents: 67819
diff changeset
   337
  def optional(entry: (String, Option[T])): Object.T =
64878
wenzelm
parents: 64761
diff changeset
   338
    entry match {
67850
3e9fe7a84b5d tuned signature;
wenzelm
parents: 67843
diff changeset
   339
      case (name, Some(x)) => Object(name -> x)
67841
8ada8f6d9495 clarified signature;
wenzelm
parents: 67819
diff changeset
   340
      case (_, None) => Object.empty
64878
wenzelm
parents: 64761
diff changeset
   341
    }
wenzelm
parents: 64761
diff changeset
   342
64545
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   343
  def value(obj: T, name: String): Option[T] =
63644
ed266398da33 support for JSON parsing;
wenzelm
parents:
diff changeset
   344
    obj match {
67841
8ada8f6d9495 clarified signature;
wenzelm
parents: 67819
diff changeset
   345
      case Object(m) => m.get(name)
63644
ed266398da33 support for JSON parsing;
wenzelm
parents:
diff changeset
   346
      case _ => None
ed266398da33 support for JSON parsing;
wenzelm
parents:
diff changeset
   347
    }
ed266398da33 support for JSON parsing;
wenzelm
parents:
diff changeset
   348
64545
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   349
  def value[A](obj: T, name: String, unapply: T => Option[A]): Option[A] =
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   350
    for {
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   351
      json <- value(obj, name)
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   352
      x <- unapply(json)
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   353
    } yield x
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   354
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   355
  def array(obj: T, name: String): Option[List[T]] =
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   356
    value(obj, name) match {
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   357
      case Some(a: List[T]) => Some(a)
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   358
      case _ => None
63644
ed266398da33 support for JSON parsing;
wenzelm
parents:
diff changeset
   359
    }
ed266398da33 support for JSON parsing;
wenzelm
parents:
diff changeset
   360
67864
449ed1afa056 prefer non-strict defaults;
wenzelm
parents: 67857
diff changeset
   361
  def value_default[A](obj: T, name: String, unapply: T => Option[A], default: => A): Option[A] =
67843
ff561f6e0a8e more operations for typed JSON values;
wenzelm
parents: 67842
diff changeset
   362
    value(obj, name) match {
ff561f6e0a8e more operations for typed JSON values;
wenzelm
parents: 67842
diff changeset
   363
      case None => Some(default)
ff561f6e0a8e more operations for typed JSON values;
wenzelm
parents: 67842
diff changeset
   364
      case Some(json) => unapply(json)
ff561f6e0a8e more operations for typed JSON values;
wenzelm
parents: 67842
diff changeset
   365
    }
64545
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   366
69458
5655af3ea5bd clarified modules and signature;
wenzelm
parents: 68943
diff changeset
   367
  def uuid(obj: T, name: String): Option[UUID.T] =
67885
839a624aabb9 prefer typed UUID;
wenzelm
parents: 67874
diff changeset
   368
    value(obj, name, Value.UUID.unapply)
839a624aabb9 prefer typed UUID;
wenzelm
parents: 67874
diff changeset
   369
64545
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   370
  def string(obj: T, name: String): Option[String] =
67842
ff87225e7e8e clarified signature;
wenzelm
parents: 67841
diff changeset
   371
    value(obj, name, Value.String.unapply)
67864
449ed1afa056 prefer non-strict defaults;
wenzelm
parents: 67857
diff changeset
   372
  def string_default(obj: T, name: String, default: => String = ""): Option[String] =
67843
ff561f6e0a8e more operations for typed JSON values;
wenzelm
parents: 67842
diff changeset
   373
    value_default(obj, name, Value.String.unapply, default)
63644
ed266398da33 support for JSON parsing;
wenzelm
parents:
diff changeset
   374
71305
2f7da37bab52 more permissive string fields;
wenzelm
parents: 71298
diff changeset
   375
  def string0(obj: T, name: String): Option[String] =
2f7da37bab52 more permissive string fields;
wenzelm
parents: 71298
diff changeset
   376
    value(obj, name, Value.String0.unapply)
2f7da37bab52 more permissive string fields;
wenzelm
parents: 71298
diff changeset
   377
  def string0_default(obj: T, name: String, default: => String = ""): Option[String] =
2f7da37bab52 more permissive string fields;
wenzelm
parents: 71298
diff changeset
   378
    value_default(obj, name, Value.String0.unapply, default)
2f7da37bab52 more permissive string fields;
wenzelm
parents: 71298
diff changeset
   379
64545
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   380
  def double(obj: T, name: String): Option[Double] =
67842
ff87225e7e8e clarified signature;
wenzelm
parents: 67841
diff changeset
   381
    value(obj, name, Value.Double.unapply)
67864
449ed1afa056 prefer non-strict defaults;
wenzelm
parents: 67857
diff changeset
   382
  def double_default(obj: T, name: String, default: => Double = 0.0): Option[Double] =
67843
ff561f6e0a8e more operations for typed JSON values;
wenzelm
parents: 67842
diff changeset
   383
    value_default(obj, name, Value.Double.unapply, default)
64545
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   384
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   385
  def long(obj: T, name: String): Option[Long] =
67842
ff87225e7e8e clarified signature;
wenzelm
parents: 67841
diff changeset
   386
    value(obj, name, Value.Long.unapply)
67864
449ed1afa056 prefer non-strict defaults;
wenzelm
parents: 67857
diff changeset
   387
  def long_default(obj: T, name: String, default: => Long = 0): Option[Long] =
67843
ff561f6e0a8e more operations for typed JSON values;
wenzelm
parents: 67842
diff changeset
   388
    value_default(obj, name, Value.Long.unapply, default)
63644
ed266398da33 support for JSON parsing;
wenzelm
parents:
diff changeset
   389
64545
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   390
  def int(obj: T, name: String): Option[Int] =
67842
ff87225e7e8e clarified signature;
wenzelm
parents: 67841
diff changeset
   391
    value(obj, name, Value.Int.unapply)
67864
449ed1afa056 prefer non-strict defaults;
wenzelm
parents: 67857
diff changeset
   392
  def int_default(obj: T, name: String, default: => Int = 0): Option[Int] =
67843
ff561f6e0a8e more operations for typed JSON values;
wenzelm
parents: 67842
diff changeset
   393
    value_default(obj, name, Value.Int.unapply, default)
63644
ed266398da33 support for JSON parsing;
wenzelm
parents:
diff changeset
   394
64545
25045094d7bb clarified JSON operations (see isabelle_vscode/a7931dc2a1ab);
wenzelm
parents: 63992
diff changeset
   395
  def bool(obj: T, name: String): Option[Boolean] =
67842
ff87225e7e8e clarified signature;
wenzelm
parents: 67841
diff changeset
   396
    value(obj, name, Value.Boolean.unapply)
67864
449ed1afa056 prefer non-strict defaults;
wenzelm
parents: 67857
diff changeset
   397
  def bool_default(obj: T, name: String, default: => Boolean = false): Option[Boolean] =
67843
ff561f6e0a8e more operations for typed JSON values;
wenzelm
parents: 67842
diff changeset
   398
    value_default(obj, name, Value.Boolean.unapply, default)
ff561f6e0a8e more operations for typed JSON values;
wenzelm
parents: 67842
diff changeset
   399
ff561f6e0a8e more operations for typed JSON values;
wenzelm
parents: 67842
diff changeset
   400
  def list[A](obj: T, name: String, unapply: T => Option[A]): Option[List[A]] =
ff561f6e0a8e more operations for typed JSON values;
wenzelm
parents: 67842
diff changeset
   401
    value(obj, name, Value.List.unapply(_, unapply))
67864
449ed1afa056 prefer non-strict defaults;
wenzelm
parents: 67857
diff changeset
   402
  def list_default[A](obj: T, name: String, unapply: T => Option[A], default: => List[A] = Nil)
67843
ff561f6e0a8e more operations for typed JSON values;
wenzelm
parents: 67842
diff changeset
   403
    : Option[List[A]] = value_default(obj, name, Value.List.unapply(_, unapply), default)
68742
a6cc4302c380 tuned signature;
wenzelm
parents: 67885
diff changeset
   404
a6cc4302c380 tuned signature;
wenzelm
parents: 67885
diff changeset
   405
  def strings(obj: T, name: String): Option[List[String]] =
71601
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 71305
diff changeset
   406
    list(obj, name, JSON.Value.String.unapply)
68742
a6cc4302c380 tuned signature;
wenzelm
parents: 67885
diff changeset
   407
  def strings_default(obj: T, name: String, default: => List[String] = Nil): Option[List[String]] =
71601
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 71305
diff changeset
   408
    list_default(obj, name, JSON.Value.String.unapply, default)
68943
e564605d4cac tuned signature;
wenzelm
parents: 68742
diff changeset
   409
e564605d4cac tuned signature;
wenzelm
parents: 68742
diff changeset
   410
  def seconds(obj: T, name: String): Option[Time] =
e564605d4cac tuned signature;
wenzelm
parents: 68742
diff changeset
   411
    value(obj, name, Value.Seconds.unapply)
e564605d4cac tuned signature;
wenzelm
parents: 68742
diff changeset
   412
  def seconds_default(obj: T, name: String, default: => Time = Time.zero): Option[Time] =
e564605d4cac tuned signature;
wenzelm
parents: 68742
diff changeset
   413
    value_default(obj, name, Value.Seconds.unapply, default)
63644
ed266398da33 support for JSON parsing;
wenzelm
parents:
diff changeset
   414
}