diff --git a/dynamodb/json.odin b/dynamodb/json.odin index 5582792..7badc22 100644 --- a/dynamodb/json.odin +++ b/dynamodb/json.odin @@ -311,7 +311,9 @@ serialize_item_to_builder :: proc(b: ^strings.Builder, item: Item) { if i > 0 { strings.write_string(b, ",") } - fmt.sbprintf(b, `"%s":`, key) + //fmt.sbprintf(b, `"%s":`, key) + write_string_escaped(b, key) + strings.write_string(b, ":") value := item[key] serialize_attribute_value(b, value) } @@ -323,7 +325,8 @@ serialize_attribute_value :: proc(b: ^strings.Builder, attr: Attribute_Value) { switch v in attr { case String: strings.write_string(b, `{"S":"`) - strings.write_string(b, string(v)) + //strings.write_string(b, string(v)) + write_string_escaped(b, string(v)) strings.write_string(b, `"}`) case DDB_Number: @@ -351,7 +354,8 @@ serialize_attribute_value :: proc(b: ^strings.Builder, attr: Attribute_Value) { if i > 0 { strings.write_string(b, ",") } - fmt.sbprintf(b, `"%s"`, s) + //fmt.sbprintf(b, `"%s"`, s) + write_string_escaped(b, s) } strings.write_string(b, "]}") @@ -403,7 +407,9 @@ serialize_attribute_value :: proc(b: ^strings.Builder, attr: Attribute_Value) { if i > 0 { strings.write_string(b, ",") } - fmt.sbprintf(b, `"%s":`, key) + //fmt.sbprintf(b, `"%s":`, key) + write_string_escaped(b, key) + strings.write_string(b, ":") value := v[key] serialize_attribute_value(b, value) } @@ -412,6 +418,29 @@ serialize_attribute_value :: proc(b: ^strings.Builder, attr: Attribute_Value) { } } +// Helper to handle escaping json strings jammed into the db +write_string_escaped :: proc(b: ^strings.Builder, s: string) { + strings.write_byte(b, '"') + for ch in transmute([]byte)s { + switch ch { + case '"': strings.write_string(b, `\"`) + case '\\': strings.write_string(b, `\\`) + case '\n': strings.write_string(b, `\n`) + case '\r': strings.write_string(b, `\r`) + case '\t': strings.write_string(b, `\t`) + case '\b': strings.write_string(b, `\b`) + case '\x0C': strings.write_string(b, `\f`) + case: + if ch < 0x20 { + fmt.sbprintf(b, `\u%04x`, int(ch)) + } else { + strings.write_byte(b, ch) + } + } + } + strings.write_byte(b, '"') +} + // ============================================================================ // Request Parsing Helpers // ============================================================================ diff --git a/jormundb.service b/jormundb.service index 01c4869..f363349 100644 --- a/jormundb.service +++ b/jormundb.service @@ -8,8 +8,8 @@ Wants=network.target Type=simple User=root -# Binary location -ExecStart=/usr/local/bin/jormundb +# Binary location. We do not want to keey alive because we are using caddy as a proxy +ExecStart=/usr/local/bin/jormundb --no-keep-alive # Environment Environment=JORMUN_HOST=127.0.0.1