Beispiel #1
0
        /// <summary>
        /// Initializes a new instance of <see cref="VowpalWabbitJsonBuilder"/>.
        /// </summary>
        public VowpalWabbitJsonBuilder(VowpalWabbitJsonSerializer serializer, IVowpalWabbitExamplePool vwPool, VowpalWabbitDefaultMarshaller defaultMarshaller, JsonSerializer jsonSerializer, int multiIndex = -1)
        {
            Contract.Requires(serializer != null);
            Contract.Requires(vw != null);
            Contract.Requires(defaultMarshaller != null);
            Contract.Requires(jsonSerializer != null);

            this.extensionState = new VowpalWabbitJsonParseState
            {
                JsonBuilder = this,
                VW          = vwPool.Native,
                MultiIndex  = multiIndex
            };

            this.namespaceStrings = new List <string>();
            this.foundMulti       = false;
            if (serializer != null)
            {
                this.referenceResolver = serializer.ReferenceResolver;
            }
            this.serializer        = serializer;
            this.vw                = vwPool.Native;
            this.defaultMarshaller = defaultMarshaller;
            this.jsonSerializer    = jsonSerializer;

            this.DefaultNamespaceContext = new VowpalWabbitMarshalContext(this.vw);
        }
        private bool HandleMultiProperty(VowpalWabbitJsonParseState state, string property)
        {
            var multiPropertyName = this.vwPool.Native.Settings.PropertyConfiguration.MultiProperty;

            if (!property.Equals(multiPropertyName, StringComparison.OrdinalIgnoreCase))
            {
                return(false);
            }

            var reader = state.Reader;

            if (!reader.Read() || reader.TokenType != JsonToken.StartArray)
            {
                throw new VowpalWabbitJsonException(reader, "Expected start array for '" + multiPropertyName + "'");
            }

            if (this.ExampleBuilders == null)
            {
                this.ExampleBuilders = new List <VowpalWabbitJsonBuilder>();
            }

            state.MultiIndex = 0;

            while (reader.Read())
            {
                switch (reader.TokenType)
                {
                case JsonToken.StartObject:
                    VowpalWabbitJsonBuilder builder = null;

                    try
                    {
                        builder = new VowpalWabbitJsonBuilder(this, this.vwPool, VowpalWabbitDefaultMarshaller.Instance, this.jsonSerializer, state.MultiIndex);
                        this.ExampleBuilders.Add(builder);
                    }
                    catch (Exception)
                    {
                        builder.Dispose();
                        throw;
                    }

                    // pass the label to the selected example
                    builder.Parse(reader, index != null && index == this.ExampleBuilders.Count - 1 ? label : null, this.extensions);

                    state.MultiIndex++;
                    break;

                case JsonToken.EndArray:
                    return(true);

                default:
                    throw new VowpalWabbitJsonException(reader, "Unexpected token: " + reader.TokenType);
                }
            }

            throw new VowpalWabbitJsonException(reader, "Unexpected end");
        }
        private bool HandleMultiProperty(VowpalWabbitJsonParseState state, string property)
        {
            var multiPropertyName = this.vwPool.Native.Settings.PropertyConfiguration.MultiProperty;
            if (!property.Equals(multiPropertyName, StringComparison.OrdinalIgnoreCase))
                return false;

            var reader = state.Reader;
            if (!reader.Read() || reader.TokenType != JsonToken.StartArray)
                throw new VowpalWabbitJsonException(reader, "Expected start array for '" + multiPropertyName + "'");

            if (this.ExampleBuilders == null)
                this.ExampleBuilders = new List<VowpalWabbitJsonBuilder>();

            while (reader.Read())
            {
                switch (reader.TokenType)
                {
                    case JsonToken.StartObject:
                        VowpalWabbitJsonBuilder builder = null;

                        try
                        {
                            builder = new VowpalWabbitJsonBuilder(this, this.vwPool, VowpalWabbitDefaultMarshaller.Instance, this.jsonSerializer);
                            this.ExampleBuilders.Add(builder);
                        }
                        catch (Exception)
                        {
                            builder.Dispose();
                            throw;
                        }

                        // pass the label to the selected example
                        builder.Parse(reader, index != null && index == this.ExampleBuilders.Count - 1 ? label : null, this.extensions);
                        break;
                    case JsonToken.EndArray:
                        return true;
                    default:
                        throw new VowpalWabbitJsonException(reader, "Unexpected token: " + reader.TokenType);
                }
            }

            throw new VowpalWabbitJsonException(reader, "Unexpected end");
        }
        /// <summary>
        /// Initializes a new instance of <see cref="VowpalWabbitJsonBuilder"/>.
        /// </summary>
        public VowpalWabbitJsonBuilder(VowpalWabbitJsonSerializer serializer, IVowpalWabbitExamplePool vwPool, VowpalWabbitDefaultMarshaller defaultMarshaller, JsonSerializer jsonSerializer)
        {
            Contract.Requires(serializer != null);
            Contract.Requires(vw != null);
            Contract.Requires(defaultMarshaller != null);
            Contract.Requires(jsonSerializer != null);

            this.extensionState = new VowpalWabbitJsonParseState
            {
                JsonBuilder = this,
                VW = vwPool.Native
            };

            this.namespaceStrings = new List<string>();
            this.foundMulti = false;
            this.referenceResolver = serializer.ReferenceResolver;
            this.serializer = serializer;
            this.vw = vwPool.Native;
            this.defaultMarshaller = defaultMarshaller;
            this.jsonSerializer = jsonSerializer;

            this.DefaultNamespaceContext = new VowpalWabbitMarshalContext(this.vw);
        }