/// <summary>
        /// Creates and configures a <see cref="IPoco"/> instance.
        /// </summary>
        /// <typeparam name="T">The type of the poco.</typeparam>
        /// <param name="this">This directory.</param>
        /// <param name="configure">Configuration action.</param>
        /// <returns>A new configured instance.</returns>
        public static T Create <T>(this PocoDirectory @this, Action <T> configure) where T : IPoco
        {
            var p = @this.Create <T>();

            configure(p);
            return(p);
        }
Beispiel #2
0
        /// <summary>
        /// Reads a <see cref="IPoco"/> (that can be null) from the Json reader
        /// that must have been written with its type.
        /// </summary>
        /// <param name="this">This directory.</param>
        /// <param name="reader">The Json reader.</param>
        /// <param name="options">The options.</param>
        /// <returns>The Poco (can be null).</returns>
        public static IPoco?Read(this PocoDirectory @this, ref Utf8JsonReader reader, PocoJsonSerializerOptions?options = null)
        {
            if (@this == null)
            {
                throw new ArgumentNullException(nameof(@this));
            }
            if (CheckNullStart(ref reader, "expecting Json Poco array or null value."))
            {
                return(null);
            }

            if (reader.TokenType != JsonTokenType.StartArray)
            {
                throw new JsonException("Expecting Json Poco array.");
            }
            reader.Read();
            string?      name = reader.GetString();
            IPocoFactory?f    = name != null? @this.Find(name) : null;

            if (f == null)
            {
                throw new JsonException($"Poco type '{name}' not found.");
            }
            reader.Read();
            var p = ((IFactoryReader)f).ReadTyped(ref reader, options);

            if (reader.TokenType != JsonTokenType.EndArray)
            {
                throw new JsonException("Expecting Json Poco end array.");
            }
            reader.Read();
            return(p);
        }
        /// <summary>
        /// Creates a new <see cref="IPoco"/> instance.
        /// </summary>
        /// <typeparam name="T">The type of the poco.</typeparam>
        /// <param name="this">This directory.</param>
        /// <returns>A new instance.</returns>
        public static T Create <T>(this PocoDirectory @this) where T : IPoco
        {
            var c = @this.Find(typeof(T));

            if (c == null)
            {
                Throw.Exception($"Unable to resolve IPoco interface '{typeof(T)}' from PocoDirectory.");
            }
            return((T)c.Create());
        }
Beispiel #4
0
 /// <summary>
 /// Reads a <see cref="IPoco"/> (that can be null) from a JSON string.
 /// The Poco must have been written with its type.
 /// </summary>
 /// <param name="this">This directory.</param>
 /// <param name="s">The string to deserialize.</param>
 /// <param name="options">The options.</param>
 /// <returns>The Poco (can be null).</returns>
 public static IPoco?JsonDeserialize(this PocoDirectory @this, string s, PocoJsonSerializerOptions?options = null)
 {
     return(JsonDeserialize(@this, System.Text.Encoding.UTF8.GetBytes(s).AsSpan(), options));
 }
Beispiel #5
0
        /// <summary>
        /// Reads a <see cref="IPoco"/> (that can be null) from Utf8 encoded bytes.
        /// The Poco must have been written with its type.
        /// </summary>
        /// <param name="this">This directory.</param>
        /// <param name="utf8Json">The utf8 encoded bytes to deserialize.</param>
        /// <param name="options">The options.</param>
        /// <returns>The Poco (can be null).</returns>
        public static IPoco?JsonDeserialize(this PocoDirectory @this, ReadOnlySpan <byte> utf8Json, PocoJsonSerializerOptions?options = null)
        {
            var r = new Utf8JsonReader(utf8Json);

            return(Read(@this, ref r, options));
        }