|
| 1 | +// Copyright [2019] LinkedIn Corp. Licensed under the Apache License, Version |
| 2 | +// 2.0 (the "License"); you may not use this file except in compliance with the |
| 3 | +// License. You may obtain a copy of the License at |
| 4 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 5 | +// |
| 6 | +// Unless required by applicable law or agreed to in writing, software |
| 7 | +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 8 | +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 9 | + |
| 10 | +package goavro |
| 11 | + |
| 12 | +import ( |
| 13 | + "fmt" |
| 14 | + "io" |
| 15 | +) |
| 16 | + |
| 17 | +// skipJSONValue advances the buffer past a single JSON value (object, array, string, number, bool, null). |
| 18 | +// This is used when IgnoreExtraFieldsFromTextual is enabled to skip over values of unknown fields. |
| 19 | +func skipJSONValue(buf []byte) ([]byte, error) { |
| 20 | + if buf, _ = advanceToNonWhitespace(buf); len(buf) == 0 { |
| 21 | + return nil, io.ErrShortBuffer |
| 22 | + } |
| 23 | + |
| 24 | + switch buf[0] { |
| 25 | + case '{': |
| 26 | + // Skip object: find matching closing brace |
| 27 | + return skipJSONObject(buf) |
| 28 | + case '[': |
| 29 | + // Skip array: find matching closing bracket |
| 30 | + return skipJSONArray(buf) |
| 31 | + case '"': |
| 32 | + // Skip string: find closing quote (handling escapes) |
| 33 | + return skipJSONString(buf) |
| 34 | + case 't': |
| 35 | + // true |
| 36 | + if len(buf) >= 4 && string(buf[:4]) == "true" { |
| 37 | + return buf[4:], nil |
| 38 | + } |
| 39 | + return nil, fmt.Errorf("cannot skip JSON value: invalid literal starting with 't'") |
| 40 | + case 'f': |
| 41 | + // false |
| 42 | + if len(buf) >= 5 && string(buf[:5]) == "false" { |
| 43 | + return buf[5:], nil |
| 44 | + } |
| 45 | + return nil, fmt.Errorf("cannot skip JSON value: invalid literal starting with 'f'") |
| 46 | + case 'n': |
| 47 | + // null |
| 48 | + if len(buf) >= 4 && string(buf[:4]) == "null" { |
| 49 | + return buf[4:], nil |
| 50 | + } |
| 51 | + return nil, fmt.Errorf("cannot skip JSON value: invalid literal starting with 'n'") |
| 52 | + default: |
| 53 | + // Must be a number (or invalid) |
| 54 | + if buf[0] == '-' || (buf[0] >= '0' && buf[0] <= '9') { |
| 55 | + return skipJSONNumber(buf) |
| 56 | + } |
| 57 | + return nil, fmt.Errorf("cannot skip JSON value: unexpected character: %q", buf[0]) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +// skipJSONObject skips a JSON object starting with '{' and returns the buffer after the closing '}'. |
| 62 | +func skipJSONObject(buf []byte) ([]byte, error) { |
| 63 | + if len(buf) == 0 || buf[0] != '{' { |
| 64 | + return nil, fmt.Errorf("cannot skip JSON object: expected '{'") |
| 65 | + } |
| 66 | + buf = buf[1:] // consume '{' |
| 67 | + var err error |
| 68 | + |
| 69 | + if buf, _ = advanceToNonWhitespace(buf); len(buf) == 0 { |
| 70 | + return nil, io.ErrShortBuffer |
| 71 | + } |
| 72 | + |
| 73 | + // Handle empty object |
| 74 | + if buf[0] == '}' { |
| 75 | + return buf[1:], nil |
| 76 | + } |
| 77 | + |
| 78 | + for len(buf) > 0 { |
| 79 | + // Skip key (string) |
| 80 | + if buf, err = skipJSONString(buf); err != nil { |
| 81 | + return nil, err |
| 82 | + } |
| 83 | + // Skip colon |
| 84 | + if buf, err = advanceAndConsume(buf, ':'); err != nil { |
| 85 | + return nil, err |
| 86 | + } |
| 87 | + // Skip value |
| 88 | + if buf, err = skipJSONValue(buf); err != nil { |
| 89 | + return nil, err |
| 90 | + } |
| 91 | + // Check for comma or closing brace |
| 92 | + if buf, _ = advanceToNonWhitespace(buf); len(buf) == 0 { |
| 93 | + return nil, io.ErrShortBuffer |
| 94 | + } |
| 95 | + switch buf[0] { |
| 96 | + case '}': |
| 97 | + return buf[1:], nil |
| 98 | + case ',': |
| 99 | + buf = buf[1:] |
| 100 | + if buf, _ = advanceToNonWhitespace(buf); len(buf) == 0 { |
| 101 | + return nil, io.ErrShortBuffer |
| 102 | + } |
| 103 | + default: |
| 104 | + return nil, fmt.Errorf("cannot skip JSON object: expected ',' or '}'; received: %q", buf[0]) |
| 105 | + } |
| 106 | + } |
| 107 | + return nil, io.ErrShortBuffer |
| 108 | +} |
| 109 | + |
| 110 | +// skipJSONArray skips a JSON array starting with '[' and returns the buffer after the closing ']'. |
| 111 | +func skipJSONArray(buf []byte) ([]byte, error) { |
| 112 | + if len(buf) == 0 || buf[0] != '[' { |
| 113 | + return nil, fmt.Errorf("cannot skip JSON array: expected '['") |
| 114 | + } |
| 115 | + buf = buf[1:] // consume '[' |
| 116 | + var err error |
| 117 | + |
| 118 | + if buf, _ = advanceToNonWhitespace(buf); len(buf) == 0 { |
| 119 | + return nil, io.ErrShortBuffer |
| 120 | + } |
| 121 | + |
| 122 | + // Handle empty array |
| 123 | + if buf[0] == ']' { |
| 124 | + return buf[1:], nil |
| 125 | + } |
| 126 | + |
| 127 | + for len(buf) > 0 { |
| 128 | + // Skip value |
| 129 | + if buf, err = skipJSONValue(buf); err != nil { |
| 130 | + return nil, err |
| 131 | + } |
| 132 | + // Check for comma or closing bracket |
| 133 | + if buf, _ = advanceToNonWhitespace(buf); len(buf) == 0 { |
| 134 | + return nil, io.ErrShortBuffer |
| 135 | + } |
| 136 | + switch buf[0] { |
| 137 | + case ']': |
| 138 | + return buf[1:], nil |
| 139 | + case ',': |
| 140 | + buf = buf[1:] |
| 141 | + if buf, _ = advanceToNonWhitespace(buf); len(buf) == 0 { |
| 142 | + return nil, io.ErrShortBuffer |
| 143 | + } |
| 144 | + default: |
| 145 | + return nil, fmt.Errorf("cannot skip JSON array: expected ',' or ']'; received: %q", buf[0]) |
| 146 | + } |
| 147 | + } |
| 148 | + return nil, io.ErrShortBuffer |
| 149 | +} |
| 150 | + |
| 151 | +// skipJSONString skips a JSON string starting with '"' and returns the buffer after the closing '"'. |
| 152 | +func skipJSONString(buf []byte) ([]byte, error) { |
| 153 | + if buf, _ = advanceToNonWhitespace(buf); len(buf) == 0 { |
| 154 | + return nil, io.ErrShortBuffer |
| 155 | + } |
| 156 | + if buf[0] != '"' { |
| 157 | + return nil, fmt.Errorf("cannot skip JSON string: expected '\"'") |
| 158 | + } |
| 159 | + buf = buf[1:] // consume opening quote |
| 160 | + |
| 161 | + for i := 0; i < len(buf); i++ { |
| 162 | + switch buf[i] { |
| 163 | + case '\\': |
| 164 | + // Skip the next character (escaped) |
| 165 | + i++ |
| 166 | + case '"': |
| 167 | + // Found closing quote |
| 168 | + return buf[i+1:], nil |
| 169 | + } |
| 170 | + } |
| 171 | + return nil, io.ErrShortBuffer |
| 172 | +} |
| 173 | + |
| 174 | +// skipJSONNumber skips a JSON number and returns the buffer after the number. |
| 175 | +func skipJSONNumber(buf []byte) ([]byte, error) { |
| 176 | + if len(buf) == 0 { |
| 177 | + return nil, io.ErrShortBuffer |
| 178 | + } |
| 179 | + |
| 180 | + i := 0 |
| 181 | + |
| 182 | + // Optional minus sign |
| 183 | + if buf[i] == '-' { |
| 184 | + i++ |
| 185 | + if i >= len(buf) { |
| 186 | + return nil, io.ErrShortBuffer |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + // Integer part |
| 191 | + switch { |
| 192 | + case buf[i] == '0': |
| 193 | + i++ |
| 194 | + case buf[i] >= '1' && buf[i] <= '9': |
| 195 | + for i < len(buf) && buf[i] >= '0' && buf[i] <= '9' { |
| 196 | + i++ |
| 197 | + } |
| 198 | + default: |
| 199 | + return nil, fmt.Errorf("cannot skip JSON number: invalid character: %q", buf[i]) |
| 200 | + } |
| 201 | + |
| 202 | + // Optional fractional part |
| 203 | + if i < len(buf) && buf[i] == '.' { |
| 204 | + i++ |
| 205 | + if i >= len(buf) || buf[i] < '0' || buf[i] > '9' { |
| 206 | + return nil, fmt.Errorf("cannot skip JSON number: expected digit after decimal point") |
| 207 | + } |
| 208 | + for i < len(buf) && buf[i] >= '0' && buf[i] <= '9' { |
| 209 | + i++ |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + // Optional exponent part |
| 214 | + if i < len(buf) && (buf[i] == 'e' || buf[i] == 'E') { |
| 215 | + i++ |
| 216 | + if i >= len(buf) { |
| 217 | + return nil, io.ErrShortBuffer |
| 218 | + } |
| 219 | + if buf[i] == '+' || buf[i] == '-' { |
| 220 | + i++ |
| 221 | + if i >= len(buf) { |
| 222 | + return nil, io.ErrShortBuffer |
| 223 | + } |
| 224 | + } |
| 225 | + if buf[i] < '0' || buf[i] > '9' { |
| 226 | + return nil, fmt.Errorf("cannot skip JSON number: expected digit in exponent") |
| 227 | + } |
| 228 | + for i < len(buf) && buf[i] >= '0' && buf[i] <= '9' { |
| 229 | + i++ |
| 230 | + } |
| 231 | + } |
| 232 | + |
| 233 | + return buf[i:], nil |
| 234 | +} |
0 commit comments