A Json Reader allowing to prefix data from a wrapped JsonReader.
상속: Newtonsoft.Json.JsonReader
예제 #1
0
        private void ParseLabel()
        {
            // peak the first property name
            if (!reader.Read())
            {
                throw new VowpalWabbitJsonException(reader.Path, "Unexpected end");
            }

            switch (reader.TokenType)
            {
            case JsonToken.StartObject:
            {
                // parse complex object
                if (!reader.Read() || reader.TokenType != JsonToken.PropertyName)
                {
                    throw new VowpalWabbitJsonException(reader.Path, "Expected at least a single property to determine the label object");
                }

                var propertyName = (string)reader.Value;

                var prefixReader = new PrefixedJsonReader(this.reader,
                                                          Tuple.Create(JsonToken.StartObject, (object)null),
                                                          Tuple.Create(JsonToken.PropertyName, (object)propertyName));

                Type labelType;
                if (!labelPropertyMapping.TryGetValue(propertyName, out labelType))
                {
                    throw new VowpalWabbitJsonException(reader.Path, "The first property ('" + propertyName + "') must match to a property of a VowpalWabbit label type.");
                }

                var label = (ILabel)jsonSerializer.Deserialize(prefixReader, labelType);

                this.defaultMarshaller.MarshalLabel(this.Context, label);
            }
            break;

            case JsonToken.Integer:
            case JsonToken.Float:
            case JsonToken.String:
            {
                // pass label directly to VW
                var labelString = reader.Value.ToString();

                this.Context.ExampleBuilder.ParseLabel(labelString);
                // prefix with label
                this.Context.AppendStringExample(false, "{0}", labelString);
            }
            break;

            case JsonToken.Null:
                // ignore
                break;

            default:
                throw new VowpalWabbitJsonException(reader.Path, "Expected label object");
            }
        }
        private void ParseLabel()
        {
            // peak the first property name
            if (!reader.Read())
                throw new VowpalWabbitJsonException(reader.Path, "Unexpected end");

            switch (reader.TokenType)
            {
                case JsonToken.StartObject:
                    {
                        // parse complex object
                        if (!reader.Read() || reader.TokenType != JsonToken.PropertyName)
                            throw new VowpalWabbitJsonException(reader.Path, "Expected at least a single property to determine the label object");

                        var propertyName = (string)reader.Value;

                        var prefixReader = new PrefixedJsonReader(this.reader,
                            Tuple.Create(JsonToken.StartObject, (object)null),
                            Tuple.Create(JsonToken.PropertyName, (object)propertyName));

                        Type labelType;
                        if (!labelPropertyMapping.TryGetValue(propertyName, out labelType))
                            throw new VowpalWabbitJsonException(reader.Path, "The first property ('" + propertyName + "') must match to a property of a VowpalWabbit label type.");

                        var label = (ILabel)jsonSerializer.Deserialize(prefixReader, labelType);

                        this.defaultMarshaller.MarshalLabel(this.Context, label);
                    }
                    break;
                case JsonToken.Integer:
                case JsonToken.Float:
                case JsonToken.String:
                    {
                        // pass label directly to VW
                        var labelString = reader.Value.ToString();

                        this.Context.ExampleBuilder.ParseLabel(labelString);
                        // prefix with label
                        this.Context.AppendStringExample(false, "{0}", labelString);
                    }
                    break;
                default:
                    throw new VowpalWabbitJsonException(reader.Path, "Expected label object");
            }
        }