public void MarshalFeature(VowpalWabbitMarshalContext context, Namespace ns, Feature feature, CustomClass value) { Assert.IsNotNull(context); Assert.IsNotNull(ns); Assert.IsNotNull(feature); Assert.IsNotNull(value); Assert.AreEqual(5, value.X); value.HasVisited = true; }
public void Marshal(VowpalWabbitMarshalContext ctx, Namespace ns, Feature feature) { var jsonSerializer = new JsonSerializer(); using (var jsonBuilder = new VowpalWabbitJsonBuilder(ctx.VW, VowpalWabbitDefaultMarshaller.Instance, jsonSerializer)) { // serialize from object to JSON var sb = new StringBuilder(); using (var writer = new JsonTextWriter(new StringWriter(sb))) { this.jsonConverter.WriteJson(writer, this.value, jsonSerializer); } // marshal from JSON to VW using (var reader = new JsonTextReader(new StringReader(sb.ToString()))) { jsonBuilder.Parse(reader, ctx, new Namespace(ctx.VW, feature.Name)); } } }
/// <summary> /// Expects that actual feature value. /// </summary> private void ParseFeature(VowpalWabbitMarshalContext context, Namespace ns, string featureName) { switch (reader.TokenType) { case JsonToken.Float: { var feature = new PreHashedFeature(this.vw, ns, featureName); this.defaultMarshaller.MarshalFeature(context, ns, feature, (double)reader.Value); } break; case JsonToken.Integer: { var feature = new PreHashedFeature(this.vw, ns, featureName); this.defaultMarshaller.MarshalFeature(context, ns, feature, (long)reader.Value); } break; case JsonToken.String: { var feature = new Feature(featureName); this.defaultMarshaller.MarshalFeatureStringEscape(context, ns, feature, (string)reader.Value); } break; case JsonToken.Boolean: { var feature = new PreHashedFeature(this.vw, ns, featureName); this.defaultMarshaller.MarshalFeature(context, ns, feature, (bool)reader.Value); } break; case JsonToken.Comment: case JsonToken.Null: // probably best to ignore? break; case JsonToken.StartArray: this.ParseFeatureArray(context, ns); break; default: throw new VowpalWabbitJsonException(reader.Path, "Unexpected token " + reader.TokenType + " while deserializing primitive feature"); } }