| ... | ... | @@ -168,8 +168,11 @@ pub fn WriteStream(comptime OutStream: type, comptime max_depth: usize) type { |
| 168 | 168 | return; |
| 169 | 169 | } |
| 170 | 170 | }, |
| 171 | | .Float => if (@floatCast(f64, value) == value) { |
| 172 | | try self.stream.print("{}", .{value}); |
| 171 | .ComptimeInt => { |
| 172 | return self.emitNumber(@as(std.math.IntFittingRange(value, value), value)); |
| 173 | }, |
| 174 | .Float, .ComptimeFloat => if (@floatCast(f64, value) == value) { |
| 175 | try self.stream.print("{}", .{@floatCast(f64, value)}); |
| 173 | 176 | self.popState(); |
| 174 | 177 | return; |
| 175 | 178 | }, |
| ... | ... | @@ -180,6 +183,7 @@ pub fn WriteStream(comptime OutStream: type, comptime max_depth: usize) type { |
| 180 | 183 | } |
| 181 | 184 | |
| 182 | 185 | pub fn emitString(self: *Self, string: []const u8) !void { |
| 186 | assert(self.state[self.state_index] == State.Value); |
| 183 | 187 | try self.writeEscapedString(string); |
| 184 | 188 | self.popState(); |
| 185 | 189 | } |
| ... | ... | @@ -191,7 +195,9 @@ pub fn WriteStream(comptime OutStream: type, comptime max_depth: usize) type { |
| 191 | 195 | |
| 192 | 196 | /// Writes the complete json into the output stream |
| 193 | 197 | pub fn emitJson(self: *Self, json: std.json.Value) Stream.Error!void { |
| 198 | assert(self.state[self.state_index] == State.Value); |
| 194 | 199 | try self.stringify(json); |
| 200 | self.popState(); |
| 195 | 201 | } |
| 196 | 202 | |
| 197 | 203 | fn indent(self: *Self) !void { |
| ... | ... | @@ -233,7 +239,32 @@ test "json write stream" { |
| 233 | 239 | defer arena_allocator.deinit(); |
| 234 | 240 | |
| 235 | 241 | var w = std.json.writeStream(out, 10); |
| 236 | | try w.emitJson(try getJson(&arena_allocator.allocator)); |
| 242 | |
| 243 | try w.beginObject(); |
| 244 | |
| 245 | try w.objectField("object"); |
| 246 | try w.emitJson(try getJsonObject(&arena_allocator.allocator)); |
| 247 | |
| 248 | try w.objectField("string"); |
| 249 | try w.emitString("This is a string"); |
| 250 | |
| 251 | try w.objectField("array"); |
| 252 | try w.beginArray(); |
| 253 | try w.arrayElem(); |
| 254 | try w.emitString("Another string"); |
| 255 | try w.arrayElem(); |
| 256 | try w.emitNumber(@as(i32, 1)); |
| 257 | try w.arrayElem(); |
| 258 | try w.emitNumber(@as(f32, 3.5)); |
| 259 | try w.endArray(); |
| 260 | |
| 261 | try w.objectField("int"); |
| 262 | try w.emitNumber(@as(i32, 10)); |
| 263 | |
| 264 | try w.objectField("float"); |
| 265 | try w.emitNumber(@as(f32, 3.5)); |
| 266 | |
| 267 | try w.endObject(); |
| 237 | 268 | |
| 238 | 269 | const result = slice_stream.getWritten(); |
| 239 | 270 | const expected = |
| ... | ... | @@ -246,38 +277,18 @@ test "json write stream" { |
| 246 | 277 | \\ "array": [ |
| 247 | 278 | \\ "Another string", |
| 248 | 279 | \\ 1, |
| 249 | | \\ 3.14e+00 |
| 280 | \\ 3.5e+00 |
| 250 | 281 | \\ ], |
| 251 | 282 | \\ "int": 10, |
| 252 | | \\ "float": 3.14e+00 |
| 283 | \\ "float": 3.5e+00 |
| 253 | 284 | \\} |
| 254 | 285 | ; |
| 255 | 286 | std.testing.expect(std.mem.eql(u8, expected, result)); |
| 256 | 287 | } |
| 257 | 288 | |
| 258 | | fn getJson(allocator: *std.mem.Allocator) !std.json.Value { |
| 259 | | var value = std.json.Value{ .Object = std.json.ObjectMap.init(allocator) }; |
| 260 | | _ = try value.Object.put("string", std.json.Value{ .String = "This is a string" }); |
| 261 | | _ = try value.Object.put("int", std.json.Value{ .Integer = @intCast(i64, 10) }); |
| 262 | | _ = try value.Object.put("float", std.json.Value{ .Float = 3.14 }); |
| 263 | | _ = try value.Object.put("array", try getJsonArray(allocator)); |
| 264 | | _ = try value.Object.put("object", try getJsonObject(allocator)); |
| 265 | | return value; |
| 266 | | } |
| 267 | | |
| 268 | 289 | fn getJsonObject(allocator: *std.mem.Allocator) !std.json.Value { |
| 269 | 290 | var value = std.json.Value{ .Object = std.json.ObjectMap.init(allocator) }; |
| 270 | 291 | _ = try value.Object.put("one", std.json.Value{ .Integer = @intCast(i64, 1) }); |
| 271 | 292 | _ = try value.Object.put("two", std.json.Value{ .Float = 2.0 }); |
| 272 | 293 | return value; |
| 273 | 294 | } |
| 274 | | |
| 275 | | fn getJsonArray(allocator: *std.mem.Allocator) !std.json.Value { |
| 276 | | var value = std.json.Value{ .Array = std.json.Array.init(allocator) }; |
| 277 | | var array = &value.Array; |
| 278 | | _ = try array.append(std.json.Value{ .String = "Another string" }); |
| 279 | | _ = try array.append(std.json.Value{ .Integer = @intCast(i64, 1) }); |
| 280 | | _ = try array.append(std.json.Value{ .Float = 3.14 }); |
| 281 | | |
| 282 | | return value; |
| 283 | | } |