Skip to content

Commit ceadce2

Browse files
committedFeb 3, 2017
fixed embedded JSON in JSON direct mode
supporting JSON objects in arrays
1 parent 4fa710d commit ceadce2

File tree

5 files changed

+63
-24
lines changed

5 files changed

+63
-24
lines changed
 

‎cs/cs_json/Serializer/JsonRawStringListConverter.cs

+2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,10 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s
4141
var valueStringEnumerable = value as List<string>;
4242
if (valueStringEnumerable != null)
4343
{
44+
writer.WriteStartArray();
4445
foreach (var str in valueStringEnumerable)
4546
writer.WriteRawValue(str);
47+
writer.WriteEndArray();
4648
return;
4749
}
4850

‎cs/cs_json/Serializer/VowpalWabbitJsonBuilder.cs

+13-5
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ internal void Parse(List<VowpalWabbitJsonParseContext> path, VowpalWabbitMarshal
165165
/// </summary>
166166
public void Parse(JsonReader reader, VowpalWabbitMarshalContext context, Namespace ns, List<VowpalWabbitJsonExtension> extensions = null)
167167
{
168+
this.namespaceStrings.Clear();
168169
this.reader = reader;
169170
this.extensions = extensions;
170171

@@ -345,9 +346,10 @@ private void ParseSpecialProperty(VowpalWabbitJsonParseContext context, string p
345346
this.foundMulti = true;
346347

347348
// forward to handler
348-
foreach (var extension in this.extensions)
349-
if (extension(this.extensionState, propertyName))
350-
return;
349+
if (this.extensions != null)
350+
foreach (var extension in this.extensions)
351+
if (extension(this.extensionState, propertyName))
352+
return;
351353

352354
// if not handled, skip it
353355
reader.Skip();
@@ -563,7 +565,7 @@ private void ParseFeature(List<VowpalWabbitJsonParseContext> path, VowpalWabbitM
563565
// probably best to ignore?
564566
break;
565567
case JsonToken.StartArray:
566-
this.WrapInNamespace(path, featureName, lastContext => this.ParseFeatureArray(lastContext.Context, lastContext.Namespace));
568+
this.WrapInNamespace(path, featureName, lastContext => this.ParseFeatureArray(path));
567569
break;
568570
default:
569571
throw new VowpalWabbitJsonException(this.reader, "Unexpected token " + reader.TokenType + " while deserializing primitive feature");
@@ -573,8 +575,11 @@ private void ParseFeature(List<VowpalWabbitJsonParseContext> path, VowpalWabbitM
573575
/// <summary>
574576
/// Expects: "1,2.2,3]" (excluding the leading [)
575577
/// </summary>
576-
private void ParseFeatureArray(VowpalWabbitMarshalContext context, Namespace ns)
578+
private void ParseFeatureArray(List<VowpalWabbitJsonParseContext> path)
577579
{
580+
var context = path.Last().Context;
581+
var ns = path.Last().Namespace;
582+
578583
ulong index = 0;
579584

580585
while (reader.Read())
@@ -587,6 +592,9 @@ private void ParseFeatureArray(VowpalWabbitMarshalContext context, Namespace ns)
587592
case JsonToken.Float:
588593
MarshalFloatFeature(context, ns, index, (float)(double)reader.Value);
589594
break;
595+
case JsonToken.StartObject:
596+
ParseProperties(path);
597+
break;
590598
case JsonToken.EndArray:
591599
return;
592600
default:

‎cs/unittest/TestJsonDirect.cs

+39-18
Original file line numberDiff line numberDiff line change
@@ -274,27 +274,48 @@ public void TestJsonConvertibleMulti()
274274
}
275275
});
276276

277-
vw.Validate(new[] {
278-
"shared | Bar:5",
279-
" | Foo:1 |A test:1.2 |B bar:2 |D d:1.2 |E e"
280-
},
281-
new JsonRawAdfString
282-
{
283-
Bar = 5,
284-
_multi = new[]
285-
{
286-
new JsonRawString
287-
{
288-
Foo = 1,
289-
Value = JsonConvert.SerializeObject(new { A = new { test = 1.2 }, B = new { bar= 2 } }),
290-
Values = new []
277+
var adf = new JsonRawString
278+
{
279+
Foo = 1,
280+
Value = JsonConvert.SerializeObject(new { A = new { test = 1.2 }, B = new { bar = 2 } }),
281+
Values = new[]
291282
{
292283
JsonConvert.SerializeObject(new { D = new { d = 1.2 } }),
293284
JsonConvert.SerializeObject(new { E = new { e = true } }),
294285
}.ToList()
295-
}
296-
}
297-
});
286+
};
287+
288+
var ctx = new JsonRawAdfString
289+
{
290+
Bar = 5,
291+
_multi = new[] { adf }
292+
};
293+
294+
vw.Validate(new[] {
295+
"shared | Bar:5",
296+
" | Foo:1 |A test:1.2 |B bar:2 |D d:1.2 |E e"
297+
}, ctx);
298+
}
299+
}
300+
301+
[TestMethod]
302+
[TestCategory("JSON")]
303+
public void TestJsonConvertible()
304+
{
305+
using (var vw = new VowpalWabbitExampleValidator<JsonRawString>(new VowpalWabbitSettings("") { TypeInspector = JsonTypeInspector.Default }))
306+
{
307+
var adf = new JsonRawString
308+
{
309+
Foo = 1,
310+
Value = JsonConvert.SerializeObject(new { A = new { test = 1.2 }, B = new { bar = 2 } }),
311+
Values = new[]
312+
{
313+
JsonConvert.SerializeObject(new { D = new { d = 1.2 } }),
314+
JsonConvert.SerializeObject(new { E = new { e = true } }),
315+
}.ToList()
316+
};
317+
318+
vw.Validate(" | Foo:1 |A test:1.2 |B bar:2 |D d:1.2 |E e", adf);
298319
}
299320
}
300321

@@ -312,7 +333,7 @@ public void TestJsonDictArray()
312333
{ "B", new float[] { 2, 3, 4.1f} }
313334
}
314335
};
315-
336+
316337
vw.Validate(" |A :1 :2 :3.1 |B :2 :3 :4.1", ex);
317338
}
318339
}

‎cs/version.props

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<PropertyGroup>
4-
<VowpalWabbitAssemblyVersion>8.3.0.6</VowpalWabbitAssemblyVersion>
4+
<VowpalWabbitAssemblyVersion>8.3.0.7</VowpalWabbitAssemblyVersion>
55
</PropertyGroup>
66
</Project>

‎vowpalwabbit/parse_example_json.h

+8
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,14 @@ class ArrayState : public BaseState<audit>
495495
return Float(ctx, (float)f);
496496
}
497497

498+
BaseState<audit>* StartObject(Context<audit>& ctx)
499+
{
500+
// parse properties
501+
ctx.PushNamespace(ctx.CurrentNamespace().name.c_str(), this);
502+
503+
return &ctx.default_state;
504+
}
505+
498506
BaseState<audit>* EndArray(Context<audit>& ctx, rapidjson::SizeType elementCount)
499507
{
500508
return ctx.PopNamespace();

0 commit comments

Comments
 (0)
Please sign in to comment.