|
1 | 1 | package vibes |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/json" |
4 | 5 | "fmt" |
| 6 | + "io" |
| 7 | + "reflect" |
| 8 | + "strings" |
5 | 9 | "time" |
6 | 10 | ) |
7 | 11 |
|
@@ -65,3 +69,165 @@ func builtinNow(exec *Execution, receiver Value, args []Value, kwargs map[string |
65 | 69 | } |
66 | 70 | return NewString(time.Now().UTC().Format(time.RFC3339)), nil |
67 | 71 | } |
| 72 | + |
| 73 | +type jsonStringifyState struct { |
| 74 | + seenArrays map[uintptr]struct{} |
| 75 | + seenHashes map[uintptr]struct{} |
| 76 | +} |
| 77 | + |
| 78 | +func builtinJSONParse(exec *Execution, receiver Value, args []Value, kwargs map[string]Value, block Value) (Value, error) { |
| 79 | + if len(args) != 1 || args[0].Kind() != KindString { |
| 80 | + return NewNil(), fmt.Errorf("JSON.parse expects a single JSON string argument") |
| 81 | + } |
| 82 | + if len(kwargs) > 0 { |
| 83 | + return NewNil(), fmt.Errorf("JSON.parse does not accept keyword arguments") |
| 84 | + } |
| 85 | + if !block.IsNil() { |
| 86 | + return NewNil(), fmt.Errorf("JSON.parse does not accept blocks") |
| 87 | + } |
| 88 | + |
| 89 | + decoder := json.NewDecoder(strings.NewReader(args[0].String())) |
| 90 | + decoder.UseNumber() |
| 91 | + |
| 92 | + var decoded any |
| 93 | + if err := decoder.Decode(&decoded); err != nil { |
| 94 | + return NewNil(), fmt.Errorf("JSON.parse invalid JSON: %v", err) |
| 95 | + } |
| 96 | + if err := decoder.Decode(&struct{}{}); err != io.EOF { |
| 97 | + return NewNil(), fmt.Errorf("JSON.parse invalid JSON: trailing data") |
| 98 | + } |
| 99 | + |
| 100 | + value, err := jsonValueToVibeValue(decoded) |
| 101 | + if err != nil { |
| 102 | + return NewNil(), err |
| 103 | + } |
| 104 | + return value, nil |
| 105 | +} |
| 106 | + |
| 107 | +func builtinJSONStringify(exec *Execution, receiver Value, args []Value, kwargs map[string]Value, block Value) (Value, error) { |
| 108 | + if len(args) != 1 { |
| 109 | + return NewNil(), fmt.Errorf("JSON.stringify expects a single value argument") |
| 110 | + } |
| 111 | + if len(kwargs) > 0 { |
| 112 | + return NewNil(), fmt.Errorf("JSON.stringify does not accept keyword arguments") |
| 113 | + } |
| 114 | + if !block.IsNil() { |
| 115 | + return NewNil(), fmt.Errorf("JSON.stringify does not accept blocks") |
| 116 | + } |
| 117 | + |
| 118 | + state := &jsonStringifyState{ |
| 119 | + seenArrays: map[uintptr]struct{}{}, |
| 120 | + seenHashes: map[uintptr]struct{}{}, |
| 121 | + } |
| 122 | + encoded, err := vibeValueToJSONValue(args[0], state) |
| 123 | + if err != nil { |
| 124 | + return NewNil(), err |
| 125 | + } |
| 126 | + |
| 127 | + payload, err := json.Marshal(encoded) |
| 128 | + if err != nil { |
| 129 | + return NewNil(), fmt.Errorf("JSON.stringify failed: %v", err) |
| 130 | + } |
| 131 | + return NewString(string(payload)), nil |
| 132 | +} |
| 133 | + |
| 134 | +func jsonValueToVibeValue(val any) (Value, error) { |
| 135 | + switch v := val.(type) { |
| 136 | + case nil: |
| 137 | + return NewNil(), nil |
| 138 | + case bool: |
| 139 | + return NewBool(v), nil |
| 140 | + case string: |
| 141 | + return NewString(v), nil |
| 142 | + case json.Number: |
| 143 | + if i, err := v.Int64(); err == nil { |
| 144 | + return NewInt(i), nil |
| 145 | + } |
| 146 | + f, err := v.Float64() |
| 147 | + if err != nil { |
| 148 | + return NewNil(), fmt.Errorf("JSON.parse invalid number %q", v.String()) |
| 149 | + } |
| 150 | + return NewFloat(f), nil |
| 151 | + case float64: |
| 152 | + return NewFloat(v), nil |
| 153 | + case []any: |
| 154 | + arr := make([]Value, len(v)) |
| 155 | + for i, item := range v { |
| 156 | + converted, err := jsonValueToVibeValue(item) |
| 157 | + if err != nil { |
| 158 | + return NewNil(), err |
| 159 | + } |
| 160 | + arr[i] = converted |
| 161 | + } |
| 162 | + return NewArray(arr), nil |
| 163 | + case map[string]any: |
| 164 | + obj := make(map[string]Value, len(v)) |
| 165 | + for key, item := range v { |
| 166 | + converted, err := jsonValueToVibeValue(item) |
| 167 | + if err != nil { |
| 168 | + return NewNil(), err |
| 169 | + } |
| 170 | + obj[key] = converted |
| 171 | + } |
| 172 | + return NewHash(obj), nil |
| 173 | + default: |
| 174 | + return NewNil(), fmt.Errorf("JSON.parse unsupported value type %T", val) |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +func vibeValueToJSONValue(val Value, state *jsonStringifyState) (any, error) { |
| 179 | + switch val.Kind() { |
| 180 | + case KindNil: |
| 181 | + return nil, nil |
| 182 | + case KindBool: |
| 183 | + return val.Bool(), nil |
| 184 | + case KindInt: |
| 185 | + return val.Int(), nil |
| 186 | + case KindFloat: |
| 187 | + return val.Float(), nil |
| 188 | + case KindString, KindSymbol: |
| 189 | + return val.String(), nil |
| 190 | + case KindArray: |
| 191 | + arr := val.Array() |
| 192 | + id := reflect.ValueOf(arr).Pointer() |
| 193 | + if id != 0 { |
| 194 | + if _, seen := state.seenArrays[id]; seen { |
| 195 | + return nil, fmt.Errorf("JSON.stringify does not support cyclic arrays") |
| 196 | + } |
| 197 | + state.seenArrays[id] = struct{}{} |
| 198 | + defer delete(state.seenArrays, id) |
| 199 | + } |
| 200 | + |
| 201 | + out := make([]any, len(arr)) |
| 202 | + for i, item := range arr { |
| 203 | + converted, err := vibeValueToJSONValue(item, state) |
| 204 | + if err != nil { |
| 205 | + return nil, err |
| 206 | + } |
| 207 | + out[i] = converted |
| 208 | + } |
| 209 | + return out, nil |
| 210 | + case KindHash, KindObject: |
| 211 | + hash := val.Hash() |
| 212 | + id := reflect.ValueOf(hash).Pointer() |
| 213 | + if id != 0 { |
| 214 | + if _, seen := state.seenHashes[id]; seen { |
| 215 | + return nil, fmt.Errorf("JSON.stringify does not support cyclic objects") |
| 216 | + } |
| 217 | + state.seenHashes[id] = struct{}{} |
| 218 | + defer delete(state.seenHashes, id) |
| 219 | + } |
| 220 | + |
| 221 | + out := make(map[string]any, len(hash)) |
| 222 | + for key, item := range hash { |
| 223 | + converted, err := vibeValueToJSONValue(item, state) |
| 224 | + if err != nil { |
| 225 | + return nil, err |
| 226 | + } |
| 227 | + out[key] = converted |
| 228 | + } |
| 229 | + return out, nil |
| 230 | + default: |
| 231 | + return nil, fmt.Errorf("JSON.stringify unsupported value type %s", val.Kind()) |
| 232 | + } |
| 233 | +} |
0 commit comments