JSON.kt 2.3 KB
Newer Older
Jonas Waeber's avatar
Jonas Waeber committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
 * search-doc-service
 * Copyright (C) 2020 Memoriav
 *
 * 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/>.
 */
Jonas Waeber's avatar
Jonas Waeber committed
18
19
package org.memobase.helpers

20
import ch.memobase.rdf.NS
Jonas Waeber's avatar
Jonas Waeber committed
21
22
23
import com.beust.klaxon.JsonArray
import com.beust.klaxon.JsonObject
import com.beust.klaxon.Klaxon
24
import com.beust.klaxon.KlaxonException
Jonas Waeber's avatar
Jonas Waeber committed
25
import java.io.StringReader
26
import org.apache.logging.log4j.LogManager
Jonas Waeber's avatar
Jonas Waeber committed
27
28
29

object JSON {

30
31
    private val log = LogManager.getLogger("JsonParser")

32
33
34
35
    private const val graph = "@graph"
    private const val Record = "Record"
    private const val RecordSet = "RecordSet"
    private const val CorporateBody = "CorporateBody"
Jonas Waeber's avatar
Jonas Waeber committed
36
37
38
39
    private const val memobaseInstitutionType = "memobaseInstitution"
    const val institutionTag = "institution"
    const val recordTag = "record"
    const val recordSetTag = "recordSet"
40

Jonas Waeber's avatar
Jonas Waeber committed
41
42
    private val klaxon = Klaxon()

43
44
45
46
47
48
49
    fun parse(data: String): JsonObject {
        return try {
            klaxon.parseJsonObject(StringReader(data))
        } catch (ex: KlaxonException) {
            log.error("${ex.localizedMessage} -> $data.")
            JsonObject()
        }
Jonas Waeber's avatar
Jonas Waeber committed
50
51
52
    }

    fun unpack(input: JsonObject): Map<String, JsonObject> {
53
        val graph = input[graph] as JsonArray<JsonObject>
Jonas Waeber's avatar
Jonas Waeber committed
54
        return graph.map {
Jonas Waeber's avatar
Jonas Waeber committed
55
56
57
58
59
60
            if (it[KEYS.atType] == NS.rico + Record) {
                Pair(recordTag, it)
            } else if (it[KEYS.atType] == NS.rico + RecordSet) {
                Pair(recordSetTag, it)
            } else if (it[KEYS.atType] == NS.rico + CorporateBody && it[KEYS.ricoType] == memobaseInstitutionType) {
                Pair(institutionTag, it)
Jonas Waeber's avatar
Jonas Waeber committed
61
            } else {
Jonas Waeber's avatar
Jonas Waeber committed
62
                Pair(it[KEYS.entityId] as String, it)
Jonas Waeber's avatar
Jonas Waeber committed
63
64
65
66
67
68
            }
        }.toMap()
    }


}