SPB Git

Repository Public

Json.swift 3.9 KB 120 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : SPIDER — Universal Web Intelligence Engine7// File             : services/ocr-worker/Sources/spider-ocr/Json.swift8// Module           : Services9// Repository       : https://git.spboucher.ai/spider10// Project Website  : Not specified11// Created          : 2026-12-0412// Last Modified    : 2026-12-0413//14// Author           : Simon-Pierre Boucher15// Position         : Professor of Finance16// Institution      : Université du Québec en Outaouais (UQO)17// Organization     : SP Boucher18// Website          : https://spboucher.ai19// Git              : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development      : dev@spboucher.ai23// Support          : support@spboucher.ai24// Security/Admin   : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License          : ISC33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041// SPIDER OCR worker — minimal deterministic JSON writer (key order preserved, shortest doubles).42import Foundation4344indirect enum J {45    case s(String)46    case n(Double)47    case i(Int)48    case b(Bool)49    case a([J])50    case o([(String, J)])51    case null5253    func write(into out: inout String) {54        switch self {55        case .s(let v): J.escape(v, into: &out)56        case .n(let v): out += v.isFinite ? (v == v.rounded() && abs(v) < 1e15 ? "\(Int64(v))" : "\(v)") : "null"57        case .i(let v): out += "\(v)"58        case .b(let v): out += v ? "true" : "false"59        case .null: out += "null"60        case .a(let items):61            out += "["62            for (k, item) in items.enumerated() {63                if k > 0 { out += "," }64                item.write(into: &out)65            }66            out += "]"67        case .o(let pairs):68            out += "{"69            for (k, pair) in pairs.enumerated() {70                if k > 0 { out += "," }71                J.escape(pair.0, into: &out)72                out += ":"73                pair.1.write(into: &out)74            }75            out += "}"76        }77    }7879    var text: String {80        var out = ""81        write(into: &out)82        return out83    }8485    private static func escape(_ v: String, into out: inout String) {86        out += "\""87        for u in v.unicodeScalars {88            switch u {89            case "\"": out += "\\\""90            case "\\": out += "\\\\"91            case "\n": out += "\\n"92            case "\r": out += "\\r"93            case "\t": out += "\\t"94            default:95                if u.value < 0x20 || u.value == 0x2028 || u.value == 0x2029 {96                    out += String(format: "\\u%04x", u.value)97                } else {98                    out.unicodeScalars.append(u)99                }100            }101        }102        out += "\""103    }104}105106/// Pixel coordinate rounded to 1/100 px.107func px(_ v: CGFloat) -> J { .n((Double(v) * 100).rounded() / 100) }108109private let outputLock = NSLock()110111/// Writes one JSON line on stdout and flushes. Exits quietly when the reader is gone.112func emit(_ value: J) {113    var line = value.text114    line += "\n"115    outputLock.lock()116    defer { outputLock.unlock() }117    let ok = line.withCString { fputs($0, stdout) } >= 0 && fflush(stdout) == 0118    if !ok { exit(0) }119}120
Files main
Menu

Theme