/// <summary> /// web api请求客户端 /// </summary> public HttpApiClient() { if (_client == null) { _client = new HttpClientAdapter(new HttpClient()); } this.JsonFormatter = new DefaultJsonFormatter(); }
/// <summary> /// Initializes a new instance of the <see cref="UserRegistrationClient" /> class. /// </summary> /// <param name="configuration">The configuration.</param> /// <param name="baasicClientFactory">The baasic client factory.</param> /// <param name="jsonFormatter">The json formatter.</param> public UserRegistrationClient(IClientConfiguration configuration, IBaasicClientFactory baasicClientFactory, IJsonFormatter jsonFormatter) : base(configuration) { BaasicClientFactory = baasicClientFactory; this.JsonFormatter = jsonFormatter; }
#pragma warning disable S3963 // "static" fields should be initialized inline static FormatterCache() { if (typeof(T).IsSmartFlagEnum(out var genericArguments)) { var formatterType = typeof(SmartFlagEnumValueFormatter <,>).MakeGenericType(genericArguments); Formatter = (IJsonFormatter <T>)Activator.CreateInstance(formatterType); } }
/// <summary> /// Initializes a new instance of the <see cref="UserPasswordRecoveryClient" /> class. /// </summary> /// <param name="configuration">The configuration.</param> /// <param name="baasicClientFactory">The baasic client factory.</param> /// <param name="jsonFormatter">The json formatter.</param> public UserPasswordRecoveryClient(IClientConfiguration configuration, IBaasicClientFactory baasicClientFactory, IJsonFormatter jsonFormatter) : base(configuration) { BaasicClientFactory = baasicClientFactory; this.JsonFormatter = jsonFormatter; }
public override byte[] Serialize <T>(T target) { var writer = new JsonWriter(); IJsonFormatter <T> formatter = Resolver.GetFormatter <T>(); formatter.Serialize(ref writer, target, Resolver); return(writer.ToUtf8ByteArray()); }
/// <summary> /// web api请求客户端 /// </summary> /// <param name="httpClient">关联的http客户端</param> public HttpApiClient(HttpClient httpClient) { RequestHost = httpClient.BaseAddress; if (_client == null) { _client = new HttpClientAdapter(httpClient); } this.JsonFormatter = new DefaultJsonFormatter(); }
static Cache() { formatter = (IJsonFormatter <T>)GetFormatter(typeof(T)); if (formatter == null) { // fallback formatter = Utf8Json.Resolvers.StandardResolver.Default.GetFormatter <T>(); } }
public static string ToJsonString <T>(this IJsonFormatter <T> formatter, T value, IJsonFormatterResolver formatterResolver) { var writer = new JsonWriter(); formatter.Serialize(ref writer, value, formatterResolver); var str = writer.ToString(); writer.Dispose(); return(str); }
/// <summary> /// Initializes a new instance of the <see cref="TokenClient" /> class. /// </summary> /// <param name="configuration">The configuration.</param> /// <param name="baasicClientFactory">The baasic client factory.</param> /// <param name="jsonFormatter">JSON formatter.</param> public TokenClient( IClientConfiguration configuration, IBaasicClientFactory baasicClientFactory, IJsonFormatter jsonFormatter ) : base(configuration) { this.BaasicClientFactory = baasicClientFactory; this.JsonFormatter = jsonFormatter; }
public StaticDictionaryFormatter(Type keyFormatterType, object[] keyFormatterArguments, Type valueFormatterType, object[] valueFormatterArguments) { keyFormatter = keyFormatterType != null ? Activator.CreateInstance(keyFormatterType, keyFormatterArguments) as IJsonFormatter <TKey> : null; valueFormatter = valueFormatterType != null ? Activator.CreateInstance(valueFormatterType, keyFormatterArguments) as IJsonFormatter <TValue> : null; }
/// <summary> /// web api请求客户端 /// </summary> /// <param name="httpClient">关联的http客户端</param> public HttpApiClient(HttpClient httpClient) { if (httpClient == null) { httpClient = new HttpClient(); } this.HttpClient = httpClient; this.JsonFormatter = new JsonFormatter(); }
static FormatterCache() { if (typeof(T) == typeof(object)) { formatter = (IJsonFormatter <T>)fallbackFormatter; } else { formatter = InnerResolver.Instance.GetFormatter <T>(); } }
static FormatterCache() { if (typeof(T) == typeof(object)) { formatter = null; } else { formatter = InnerResolver.Instance.GetFormatter <T>(); } }
/// <summary> /// web api请求客户端 /// </summary> /// <param name="httpClient">关联的http客户端</param> public HttpApiClient(HttpClient httpClient) { RequestHost = httpClient.BaseAddress; if (_client == null) { httpClient.Timeout = TimeSpan.FromSeconds(10); httpClient.DefaultRequestHeaders.Connection.Add("keep-alive"); _client = new HttpClientAdapter(httpClient); } this.JsonFormatter = new DefaultJsonFormatter(); }
static FormatterCache() { if (typeof(T) != typeof(object) && (typeof(T).IsSealed || typeof(T).FullName.StartsWith("System"))) { formatter = InnerResolver.Instance.GetFormatter <T>(); } else { formatter = new DynamicObjectTypeFallbackFormatterAdapter <T>(fallbackFormatter); } }
static FormatterCache() { if (typeof(T) == typeof(object)) { formatter = (IJsonFormatter <T>)ObjectFallbackFormatter; } else { formatter = AllowPrivateExcludeNullSnakeCaseStandardResolverCore.Instance.GetFormatter <T>(); } }
public StaticNullableFormatter(Type formatterType, object[] formatterArguments) { try { underlyingFormatter = (IJsonFormatter <T>)Activator.CreateInstance(formatterType, formatterArguments); } catch (Exception ex) { throw new InvalidOperationException("Can not create formatter from JsonFormatterAttribute, check the target formatter is public and has constructor with right argument. FormatterType:" + formatterType.Name, ex); } }
static FormatterCache() { foreach (var resolver in InnerResolvers) { Formatter = resolver.GetFormatter <T>(); if (Formatter != null) { break; } } }
/// <summary> /// web api请求客户端 /// </summary> /// <param name="baseUrl"></param> public HttpApiClient(string baseUrl) { if (!string.IsNullOrEmpty(baseUrl)) { RequestHost = new Uri(baseUrl); } if (_client == null) { _client = new HttpClientAdapter(new HttpClient()); } this.JsonFormatter = new DefaultJsonFormatter(); }
public IJsonFormatter[] CollectCurrentRegisteredFormatters() { var formatterArrayArray = new IJsonFormatter[resolvers.Length][]; var totalLength = 0; for (var index = 0; index < resolvers.Length; index++) { var resolver = resolvers[index]; ref var formatterArray = ref formatterArrayArray[index]; formatterArrayArray[index] = resolver.CollectCurrentRegisteredFormatters(); totalLength += formatterArray.Length; }
public override T Deserialize <T>(byte[] data) { if (data == null) { throw new ArgumentNullException(nameof(data)); } var reader = new JsonReader(data); IJsonFormatter <T> formatter = Resolver.GetFormatter <T>(); return(formatter.Deserialize(ref reader, Resolver)); }
static FormatterCache() { foreach (var item in resolvers) { var f = item.GetFormatter <T>(); if (f != null) { formatter = f; return; } } }
public override string Serialize <T>(T target) { var writer = new JsonWriter(); IJsonFormatter <T> formatter = Resolver.GetFormatter <T>(); formatter.Serialize(ref writer, target, Resolver); string text = Readable ? JsonSerializer.PrettyPrint(writer.ToUtf8ByteArray()) : writer.ToString(); return(text); }
public override T Deserialize <T>(string data) { if (data == null) { throw new ArgumentNullException(nameof(data)); } byte[] bytes = Encoding.UTF8.GetBytes(data); var reader = new JsonReader(bytes); IJsonFormatter <T> formatter = Resolver.GetFormatter <T>(); return(formatter.Deserialize(ref reader, Resolver)); }
public void Serialize(ref JsonWriter writer, IMultiGetRequest value, IJsonFormatterResolver formatterResolver) { writer.WriteBeginObject(); if (!(value?.Documents.HasAny()).GetValueOrDefault(false)) { writer.WriteEndObject(); return; } var docs = value.Documents.Select(d => { if (value.Index != null) { d.Index = null; } return(d); }) .ToList(); var flatten = docs.All(p => p.CanBeFlattened); writer.WritePropertyName(flatten ? "ids" : "docs"); IJsonFormatter <IMultiGetOperation> formatter = null; if (!flatten) { formatter = formatterResolver.GetFormatter <IMultiGetOperation>(); } writer.WriteBeginArray(); for (var index = 0; index < docs.Count; index++) { if (index > 0) { writer.WriteValueSeparator(); } var id = docs[index]; if (flatten) { IdFormatter.Serialize(ref writer, id.Id, formatterResolver); } else { formatter.Serialize(ref writer, id, formatterResolver); } } writer.WriteEndArray(); writer.WriteEndObject(); }
static InnerFormatterCache() { foreach (var item in Resolvers) { var f = item.GetFormatter <T>(); if (f == null) { continue; } Formatter = f; return; } }
/// <summary> /// web api请求客户端 /// </summary> public HttpApiClient() { if (_client == null) { var httpClient = new HttpClient { Timeout = TimeSpan.FromSeconds(10) }; httpClient.DefaultRequestHeaders.Connection.Add("keep-alive"); _client = new HttpClientAdapter(httpClient); } this.JsonFormatter = new DefaultJsonFormatter(); }
static FormatterCache() { var type = typeof(T); if (type == typeof(object)) { return; } (SerializeFunctionPointer, DeserializeFunctionPointer, CalcByteLengthForSerializationFunctionPointer, SerializeSpanFunctionPointer) = BasicGenericsResolverGetFormatterHelper.GetFunctionPointers(type); Formatter = BasicGenericsResolverGetFormatterHelper.CreateFormatter(type) as IJsonFormatter <T>; formatterTable.TryAdd(new ThreadSafeTypeKeyReferenceHashTable <IJsonFormatter> .Entry(type, Formatter)); }
/// <summary> /// web api请求客户端 /// </summary> /// <param name="httpClient">关联的http客户端</param> /// <param name="baseUrl"></param> public HttpApiClient(IHttpClient httpClient, string baseUrl) { if (!string.IsNullOrEmpty(baseUrl)) { RequestHost = baseUrl; } if (_client == null) { //httpClient.Timeout = TimeSpan.FromSeconds(10); //httpClient.DefaultRequestHeaders.Connection.Add("keep-alive"); _client = httpClient; } this.JsonFormatter = new DefaultJsonFormatter(); }
/// <summary> /// Initializes a new instance of the <see cref="BaasicClient" /> class. /// </summary> /// <param name="configuration">The configuration.</param> /// <param name="httpClientFactory">The HTTP client factory.</param> /// <param name="jsonFormatter">JSON formatter.</param> public BaasicClient(IClientConfiguration configuration, IHttpClientFactory httpClientFactory, IJsonFormatter jsonFormatter ) { Configuration = configuration; HttpClientFactory = httpClientFactory; JsonFormatter = jsonFormatter; this._client = new Lazy <HttpClient>(() => { var client = HttpClientFactory.Create(); InitializeClient(client, Configuration.DefaultMediaType); return(client); }, true); }
public virtual void AddFormatter(Type type, IJsonFormatter formatter, bool includeSubclasses) { if (type == null) throw new ArgumentNullException("type"); if (formatter == null) throw new ArgumentNullException("formatter"); if (type.IsInterface || type.IsAbstract) includeSubclasses = true; if (includeSubclasses) _lazyFormatters.Add(type, formatter); else _formatters.Add(type, formatter); }
public void AddFormatter(Type type, IJsonFormatter formatter) { AddFormatter(type, formatter, false); }