File src/remiaudio/xspf/json.cr from the latest check-in


     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
   100
   101
   102
   103
#### RemiAudio
#### Copyright (C) 2022-2024 Remilia Scarlet <remilia@posteo.jp>
####
#### This program is free software: you can redistribute it and/or modify it
#### under the terms of the GNU Affero General Public License as published by
#### the Free Software Foundation, either version 3 of the License, or (at your
#### option) any later version.
####
#### This program is distributed in the hope that it will be useful, but WITHOUT
#### ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
#### FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
#### License for more details.
####
#### You should have received a copy of the GNU Affero General Public License
#### along with this program.  If not, see <https://www.gnu.org/licenses/>.
require "json"
require "uri"
require "xml"

module RemiAudio::Xspf
  module XmlDateConverter
    def self.from_json(pull : JSON::PullParser) : Time
      Time.parse_rfc3339(pull.read_string)
    end

    def self.to_json(value : Time, json : JSON::Builder) : Nil
      value.to_rfc3339.to_json(json)
    end
  end

  module AttributionJsonConverter
    def self.from_json(pull : JSON::PullParser) : Attribution?
      ret : Attribution? = nil
      pull.read_array do
        ret = Attribution.new unless ret
        pull.read_begin_object
        case pull.read_object_key
        when "identifier" then ret.identifiers << URI.parse(pull.read_string)
        when "location" then ret.locations << URI.parse(pull.read_string)
        else raise Error.new("Bad JSON, 'attribution' element has unexpected data at #{pull.line_number}")
        end
        pull.read_end_object
      end
      ret
    end

    def self.to_json(value : Attribution, json : JSON::Builder) : Nil
      json.array do
        value.locations.each do |loc|
          json.object { json.field("location", loc.to_s) }
        end

        value.identifiers.each do |ident|
          json.object { json.field("identifier", ident.to_s) }
        end
      end
    end
  end

  module LinksJsonConverter
    def self.from_json(pull : JSON::PullParser) : Array(Link)
      ret = [] of Link
      pull.read_array do
        pull.read_begin_object
        rel = pull.read_object_key
        lnk = pull.read_string
        ret << Link.new(URI.parse(rel), URI.parse(lnk))
        pull.read_end_object
      end
      ret
    end

    def self.to_json(value : Array(Link), json : JSON::Builder) : Nil
      json.array do
        value.each do |lnk|
          json.object { json.field(lnk.rel.to_s, lnk.content.to_s) }
        end
      end
    end
  end

  module MetasJsonConverter
    def self.from_json(pull : JSON::PullParser) : Array(Meta)
      ret = [] of Meta
      pull.read_array do
        pull.read_begin_object
        rel = pull.read_object_key
        meta = pull.read_string
        ret << Meta.new(URI.parse(rel), meta)
        pull.read_end_object
      end
      ret
    end

    def self.to_json(value : Array(Meta), json : JSON::Builder) : Nil
      json.array do
        value.each do |meta|
          json.object { json.field(meta.rel.to_s, meta.content.to_s) }
        end
      end
    end
  end
end