public void SerializeDeserializeVirtualMachine() { var helper = new JsonHelpers<VirtualMachine>(); var toSerialize = new VirtualMachine { ID = Guid.NewGuid(), StampId = Guid.NewGuid(), CreationTime = DateTime.Now, PerfDiskBytesRead = 500, Name = @"This is a test 这是一个测试 㐀㐁㐂㐃㐄㐅" }; var serialized = helper.Serialize(toSerialize); var vmList = helper.Deserialize(serialized); Assert.NotNull(vmList); Assert.Equal(vmList.Count, 1); Assert.True(vmList[0] is VirtualMachine); Assert.Equal(toSerialize.ID, vmList[0].ID); Assert.Equal(toSerialize.StampId, vmList[0].StampId); Assert.Equal(toSerialize.CreationTime, vmList[0].CreationTime); Assert.Equal(toSerialize.PerfDiskBytesRead, vmList[0].PerfDiskBytesRead); Assert.Equal(toSerialize.Name, vmList[0].Name); }
internal void InitializeForSourceGen( JsonSerializerOptions options, bool isProperty, bool isPublic, Type declaringType, JsonTypeInfo typeInfo, JsonConverter <T> converter, Func <object, T?>?getter, Action <object, T?>?setter, JsonIgnoreCondition?ignoreCondition, bool hasJsonInclude, JsonNumberHandling?numberHandling, string propertyName, string?jsonPropertyName) { Options = options; ClrName = propertyName; // Property name settings. if (jsonPropertyName != null) { NameAsString = jsonPropertyName; } else if (options.PropertyNamingPolicy == null) { NameAsString = ClrName; } else { NameAsString = options.PropertyNamingPolicy.ConvertName(ClrName); if (NameAsString == null) { ThrowHelper.ThrowInvalidOperationException_SerializerPropertyNameNull(DeclaringType, this); } } NameAsUtf8Bytes ??= Encoding.UTF8.GetBytes(NameAsString !); EscapedNameSection ??= JsonHelpers.GetEscapedPropertyNameSection(NameAsUtf8Bytes, Options.Encoder); SrcGen_IsPublic = isPublic; SrcGen_HasJsonInclude = hasJsonInclude; DeclaredPropertyType = typeof(T); ConverterBase = converter; if (ignoreCondition == JsonIgnoreCondition.Always) { IsIgnored = true; Debug.Assert(!ShouldSerialize); Debug.Assert(!ShouldDeserialize); } else { Get = getter !; Set = setter; HasGetter = Get != null; HasSetter = Set != null; RuntimeTypeInfo = typeInfo; DeclaringType = declaringType; IgnoreCondition = ignoreCondition; MemberType = isProperty ? MemberTypes.Property : MemberTypes.Field; _converterIsExternalAndPolymorphic = !converter.IsInternalConverter && DeclaredPropertyType != converter.TypeToConvert; PropertyTypeCanBeNull = typeof(T).CanBeNull(); _propertyTypeEqualsTypeToConvert = converter.TypeToConvert == typeof(T); ConverterStrategy = Converter !.ConverterStrategy; RuntimePropertyType = DeclaredPropertyType; DetermineIgnoreCondition(IgnoreCondition); // TODO: this method needs to also take the number handling option for the declaring type. DetermineNumberHandlingForProperty(numberHandling, declaringTypeNumberHandling: null); DetermineSerializationCapabilities(IgnoreCondition); } }
public async Task DoWorkAsync_Should_Return_Success_When_Blob_Present_But_No_Metadata() { // Arrange var topicEndpointUri = new Uri("https://www.topichost.com"); var opCtx = JsonHelpers.JsonToJObject("{\"someIdType1\":\"someValue1\", \"someIdType2\":\"someValue2\"}"); var testEvent = new EventGridEvent { EventTime = DateTime.UtcNow, EventType = CustomEventTypes.RequestBlobDelete, DataVersion = "1.0", Data = JsonConvert.SerializeObject(new RequestBlobDeleteDTO { BlobUri = new Uri(_expectedInboxUrl), OperationContext = opCtx, }), }; EventGridEvent publishedEvent = null; // Arrange Mocks Mock.Get(_settingsProvider) .Setup(x => x.GetAppSettingsValue(Publishing.TopicOutboundEndpointSettingName)) .Returns(topicEndpointUri.ToString()); Mock.Get(_storageService) .Setup(x => x.GetBlobMetadataAsync(It.IsAny <Uri>(), It.IsAny <StorageClientProviderContext>())) .ReturnsAsync((JObject)null); Mock.Get(_storageService) .Setup(x => x.BlobDelete(It.IsAny <Uri>(), It.IsAny <StorageClientProviderContext>())) .ReturnsAsync(true); Mock.Get(_eventGridPublisher) .Setup(x => x.PublishEventToTopic(It.IsAny <EventGridEvent>())) .Callback <EventGridEvent>((eventGridEvent) => publishedEvent = eventGridEvent) .ReturnsAsync(true); // Act: var handleAsyncResult = await _handler.HandleAsync(testEvent).ConfigureAwait(true); // Assert: handleAsyncResult.ShouldBe(true, "handleAsync should always return true"); // Check that nothing (informational okay) was logged. // No exceptions or exception objects Mock.Get(_logger).Verify(x => x.LogException(It.IsAny <EventId>(), It.IsAny <Exception>(), It.IsAny <string>(), It.IsAny <object[]>()), Times.Never, "No exceptions"); Mock.Get(_logger).Verify(x => x.LogExceptionObject(It.IsAny <EventId>(), It.IsAny <Exception>(), It.IsAny <object>()), Times.Never, "No exception objects"); // No criticals, note the specific LogEventIds don't matter here Mock.Get(_logger).Verify(x => x.LogEvent(It.Is <EventId>(e => e.GetLevelName() == LogEventIds.NoSuchBlobInBlobDeleteHandler.GetLevelName()), It.IsAny <string>(), It.IsAny <object[]>()), Times.Never, "No Criticals [1]"); Mock.Get(_logger).Verify(x => x.LogEventObject(It.Is <EventId>(e => e.GetLevelName() == LogEventIds.NoSuchBlobInBlobDeleteHandler.GetLevelName()), It.IsAny <object>()), Times.Never, "No Criticals [2]"); // No Errors, note the specific LogEventIds don't matter here Mock.Get(_logger).Verify(x => x.LogEvent(It.Is <EventId>(e => e.GetLevelName() == LogEventIds.FailedToCreateBlobDeletedDataWithEventDataInBlobDeletedHandler.GetLevelName()), It.IsAny <string>(), It.IsAny <object[]>()), Times.Never, "No Errors [1]"); Mock.Get(_logger).Verify(x => x.LogEventObject(It.Is <EventId>(e => e.GetLevelName() == LogEventIds.FailedToCreateBlobDeletedDataWithEventDataInBlobDeletedHandler.GetLevelName()), It.IsAny <object>()), Times.Never, "No Errors [2]"); // No warning that the blob doesn't exist, note the specific LogEventIds don't matter here Mock.Get(_logger).Verify(x => x.LogEvent(It.Is <EventId>(e => e.GetLevelName() == LogEventIds.NoSuchBlobInBlobDeleteHandler.GetLevelName()), It.IsAny <string>(), It.IsAny <object[]>()), Times.Never, "No Warnings [1]"); Mock.Get(_logger).Verify(x => x.LogEventObject(It.Is <EventId>(e => e.GetLevelName() == LogEventIds.NoSuchBlobInBlobDeleteHandler.GetLevelName()), It.IsAny <object>()), Times.Never, "No Warnings [2]"); Mock.Get(_storageService).Verify(x => x.GetBlobMetadataAsync(It.IsAny <Uri>(), It.IsAny <StorageClientProviderContext>()), Times.Once, "Should attempt to get blob metadata."); Mock.Get(_storageService).Verify(x => x.BlobDelete(It.IsAny <Uri>(), It.IsAny <StorageClientProviderContext>()), Times.Once, "Should attempt to delete the blob."); Mock.Get(_eventGridPublisher) .Verify(x => x.PublishEventToTopic(It.IsAny <EventGridEvent>()), Times.Exactly(2), "Should only publish ACK + Failure."); // Assert publishedEvent: publishedEvent.EventType.ShouldBe(CustomEventTypes.ResponseBlobDeleteScheduled); publishedEvent.EventTime.ShouldBeInRange(testEvent.EventTime, testEvent.EventTime.AddMinutes(1)); publishedEvent.Data.ShouldBeOfType(typeof(ResponseBlobDeleteScheduledDTO)); var data = (ResponseBlobDeleteScheduledDTO)publishedEvent.Data; data.ShouldNotBeNull(); data.OperationContext.ContainsKey("someIdType1").ShouldBeTrue(); data.OperationContext["someIdType1"].ShouldBe("someValue1"); data.OperationContext.ContainsKey("someIdType2").ShouldBeTrue(); data.OperationContext["someIdType2"].ShouldBe("someValue2"); data.BlobUri.ToString().ShouldBe(_expectedInboxUrl); // Metadata should be empty data.BlobMetadata.ShouldNotBeNull(); data.BlobMetadata.Count.ShouldBe(0); // empty JObject. }
public async Task DoWorkAsync_ShouldReturnTrueAndNotLog_WhenNoErrors() { // Arrange var topicEndpointUri = new Uri("https://www.topichost.com"); var opCtx = JsonHelpers.JsonToJObject("{\"someIdType1\":\"someValue1\", \"someIdType2\":\"someValue2\"}"); var opCtxString = JsonHelpers.SerializeOperationContext(opCtx); var testEvent = new EventGridEvent { EventTime = DateTime.UtcNow, EventType = CustomEventTypes.RequestBlobDelete, DataVersion = "1.0", Data = JsonConvert.SerializeObject(new RequestBlobDeleteDTO { BlobUri = new Uri(_expectedInboxUrl), OperationContext = opCtx, }), }; var expectedBlobMetadata = JsonHelpers.JsonToJObject("{\"someProp1\":\"someValue1\", \"someProp2\":\"someValue2\"}", true); var ctx = StorageClientProviderContext.None; EventGridEvent publishedEvent = null; // Arrange Mocks Mock.Get(_settingsProvider) .Setup(x => x.GetAppSettingsValue(Publishing.TopicOutboundEndpointSettingName)) .Returns(topicEndpointUri.ToString()); Mock.Get(_storageService) .Setup(x => x.GetBlobMetadataAsync(It.IsAny <Uri>(), It.IsAny <StorageClientProviderContext>())) .ReturnsAsync(expectedBlobMetadata); Mock.Get(_storageService) .Setup(x => x.BlobDelete(It.IsAny <Uri>(), It.IsAny <StorageClientProviderContext>())) .ReturnsAsync(true); Mock.Get(_eventGridPublisher) .Setup(x => x.PublishEventToTopic(It.IsAny <EventGridEvent>())) .Callback <EventGridEvent>((eventGridEvent) => publishedEvent = eventGridEvent) .ReturnsAsync(true); // Act: var handleAsyncResult = await _handler.HandleAsync(testEvent).ConfigureAwait(true); // Assert: handleAsyncResult.ShouldBe(true, "handleAsync should always return true"); Mock.Get(_logger).Verify(x => x.LogEventObject(LogEventIds.InvalidUriInBlobDeleteHandler, It.IsAny <object>()), Times.Never, "An exception should NOT be logged when the publishing succeeds [2]"); Mock.Get(_logger).Verify(x => x.LogExceptionObject(It.IsAny <EventId>(), It.IsAny <Exception>(), It.IsAny <object>()), Times.Never, "No exceptions should be logged when the publishing succeeds [3]"); Mock.Get(_logger).Verify(x => x.LogEvent(LogEventIds.NoSuchBlobInBlobDeleteHandler, It.IsAny <string>(), It.IsAny <Uri>()), Times.Never, "An exception should NOT be logged when the publishing succeeds [4]"); Mock.Get(_storageService).Verify(x => x.GetBlobMetadataAsync(It.IsAny <Uri>(), It.IsAny <StorageClientProviderContext>()), Times.Once, "Should attempt to get blob metadata."); Mock.Get(_storageService).Verify(x => x.BlobDelete(It.IsAny <Uri>(), It.IsAny <StorageClientProviderContext>()), Times.Once, "Should attempt to delete the blob."); Mock.Get(_eventGridPublisher) .Verify(x => x.PublishEventToTopic(It.IsAny <EventGridEvent>()), Times.Exactly(2), "Should only publish ACK + Scheduled."); // Assert publishedEvent: publishedEvent.EventType.ShouldBe(CustomEventTypes.ResponseBlobDeleteScheduled); publishedEvent.EventTime.ShouldBeInRange(testEvent.EventTime, testEvent.EventTime.AddMinutes(1)); publishedEvent.Data.ShouldBeOfType(typeof(ResponseBlobDeleteScheduledDTO)); var data = (ResponseBlobDeleteScheduledDTO)publishedEvent.Data; data.ShouldNotBeNull(); data.OperationContext.ContainsKey("someIdType1").ShouldBeTrue(); data.OperationContext["someIdType1"].ShouldBe("someValue1"); data.OperationContext.ContainsKey("someIdType2").ShouldBeTrue(); data.OperationContext["someIdType2"].ShouldBe("someValue2"); data.BlobUri.ToString().ShouldBe(_expectedInboxUrl); data.BlobMetadata.ShouldNotBeNull(); data.BlobMetadata.ContainsKey("someProp1").ShouldBeTrue(); data.BlobMetadata["someProp1"].ShouldBe("someValue1"); data.BlobMetadata.ContainsKey("someProp2").ShouldBeTrue(); data.BlobMetadata["someProp2"].ShouldBe("someValue2"); }
// todo: for readability, refactor this method to split by ClassType(Enumerable, Object, or Value) like Write() private static void ReadCore( JsonSerializerOptions options, ref Utf8JsonReader reader, ref ReadStack state) { while (reader.Read()) { JsonTokenType tokenType = reader.TokenType; if (JsonHelpers.IsInRangeInclusive(tokenType, JsonTokenType.String, JsonTokenType.False)) { Debug.Assert(tokenType == JsonTokenType.String || tokenType == JsonTokenType.Number || tokenType == JsonTokenType.True || tokenType == JsonTokenType.False); if (HandleValue(tokenType, options, ref reader, ref state)) { return; } } else if (tokenType == JsonTokenType.PropertyName) { if (!state.Current.Drain) { Debug.Assert(state.Current.ReturnValue != default); Debug.Assert(state.Current.JsonClassInfo != default); ReadOnlySpan <byte> propertyName = reader.HasValueSequence ? reader.ValueSequence.ToArray() : reader.ValueSpan; state.Current.JsonPropertyInfo = state.Current.JsonClassInfo.GetProperty(options, propertyName, ref state.Current); if (state.Current.JsonPropertyInfo == null) { state.Current.JsonPropertyInfo = s_missingProperty; } state.Current.PropertyIndex++; } } else if (tokenType == JsonTokenType.StartObject) { HandleStartObject(options, ref state); } else if (tokenType == JsonTokenType.EndObject) { if (HandleEndObject(options, ref state)) { return; } } else if (tokenType == JsonTokenType.StartArray) { HandleStartArray(options, ref reader, ref state); } else if (tokenType == JsonTokenType.EndArray) { if (HandleEndArray(options, ref state)) { return; } } else if (tokenType == JsonTokenType.Null) { if (HandleNull(ref reader, ref state, options)) { return; } } } return; }
public CTDAFunctions(DefinitionManager manager, JObject src) : base(manager, src) { ctdaFunctions = JsonHelpers.Defs <CTDAFunction>(manager, src, "ctdaFunctions"); }
public void ConvertYaml(FormatType format) { string result = JsonHelpers.ConvertToFormat(_baseJson, format); CompareConvertResult(result, format); }
private async void WatcherOnPositionChanged(Geolocator sender, PositionChangedEventArgs e) { if (DateTime.Now.TimeOfDay.Subtract(_lastTimeSend) >= _minSendTime) { _geozoneRequest.Lat = e.Position.Coordinate.Latitude; _geozoneRequest.Lon = e.Position.Coordinate.Longitude; var webRequest = (HttpWebRequest)HttpWebRequest.Create(Constants.GeozoneUrl); webRequest.Method = "POST"; webRequest.ContentType = "application/x-www-form-urlencoded"; string request = String.Format("{{ \"request\":{0}}}", JsonConvert.SerializeObject(_geozoneRequest)); byte[] requestBytes = System.Text.Encoding.UTF8.GetBytes(request); // Write the channel URI to the request stream. Stream requestStream = await webRequest.GetRequestStreamAsync(); requestStream.Write(requestBytes, 0, requestBytes.Length); try { // Get the response from the server. WebResponse response = await webRequest.GetResponseAsync(); StreamReader requestReader = new StreamReader(response.GetResponseStream()); String webResponse = requestReader.ReadToEnd(); string errorMessage = String.Empty; Debug.WriteLine("Response: " + webResponse); JObject jRoot = JObject.Parse(webResponse); int code = JsonHelpers.GetStatusCode(jRoot); if (JsonHelpers.GetStatusCode(jRoot) == 200) { double dist = jRoot["response"].Value <double>("distance"); if (dist > 0) { _watcher.MovementThreshold = dist / 2; } } else { errorMessage = JsonHelpers.GetStatusMessage(jRoot); } if (!String.IsNullOrEmpty(errorMessage) && OnError != null) { Debug.WriteLine("Error: " + errorMessage); OnError(this, new CustomEventArgs <string> { Result = errorMessage }); } } catch (Exception ex) { Debug.WriteLine("Error: " + ex.Message); OnError(this, new CustomEventArgs <string> { Result = ex.Message }); } _lastTimeSend = DateTime.Now.TimeOfDay; } }
private static List <NewsStreamItem> ProcessNewsStreamItems(string result) { var returnType = JsonHelpers.DeserializeJson <List <NewsStreamItem> >(result); return(returnType); }
public ObservableCollection <NewsStreamItem> GetNewsStream(string team, string start, string pageSize) { _newsStream = new ObservableCollection <NewsStreamItem>(); var loadedEventArgs = new LoadEventArgs(); string queryString = string.Format("{0}?team={1}&start={2}&pageSize={3}", Settings.StreamUrl, team ?? "", start ?? "", pageSize ?? ""); var wb = new SharpGIS.GZipWebClient(); Observable.FromEvent <DownloadStringCompletedEventArgs>(wb, "DownloadStringCompleted") // Let's make sure that we’re on the thread pool .ObserveOn(Scheduler.ThreadPool) // When the event fires, just select the string and make // an IObservable<string> instead .Select(newString => ProcessNewsStreamItems(newString.EventArgs.Result)) // Now go back to the UI Thread .ObserveOn(Scheduler.Dispatcher) // Subscribe to the observable .Subscribe(s => { loadedEventArgs.IsLoaded = true; loadedEventArgs.Message = ""; for (int index = 0; index < s.Count; index++) { NewsStreamItem newsStreamItem = s[index]; newsStreamItem.Title = Regex.Replace(newsStreamItem.Title, ""); _newsStream.Add(newsStreamItem); } OnNewsStreamLoaded(loadedEventArgs); if (!(App.isoSettings.Contains("NewStream"))) { ThreadPool.QueueUserWorkItem(o => { var json = JsonHelpers.SerializeJson(_newsStream); App.isoSettings.Add("NewStream", json); }); } else { ThreadPool.QueueUserWorkItem(o => { var json = JsonHelpers.SerializeJson(_newsStream); App.isoSettings["NewStream"] = json; }); } }, e => { loadedEventArgs.IsLoaded = false; //TODO: LOG Error loadedEventArgs.Message = "We were unable to retrieve the news feed. Please refresh"; OnNewsStreamLoaded(loadedEventArgs); } ); wb.DownloadStringAsync(new Uri(queryString)); return(_newsStream); }
private static NewsFeature ProcessFeatures(string result) { var returnType = JsonHelpers.DeserializeJson <NewsFeature>(result); return(returnType); }
private static NewsFeedModel ProcessNews(string result) { return(JsonHelpers.DeserializeJson <NewsFeedModel>(result)); }
internal void InitializeForSourceGen(JsonSerializerOptions options, JsonPropertyInfoValues <T> propertyInfo) { Options = options; ClrName = propertyInfo.PropertyName; // Property name settings. if (propertyInfo.JsonPropertyName != null) { NameAsString = propertyInfo.JsonPropertyName; } else if (options.PropertyNamingPolicy == null) { NameAsString = ClrName; } else { NameAsString = options.PropertyNamingPolicy.ConvertName(ClrName); if (NameAsString == null) { ThrowHelper.ThrowInvalidOperationException_SerializerPropertyNameNull(DeclaringType, this); } } NameAsUtf8Bytes ??= Encoding.UTF8.GetBytes(NameAsString !); EscapedNameSection ??= JsonHelpers.GetEscapedPropertyNameSection(NameAsUtf8Bytes, Options.Encoder); SrcGen_IsPublic = propertyInfo.IsPublic; SrcGen_HasJsonInclude = propertyInfo.HasJsonInclude; SrcGen_IsExtensionData = propertyInfo.IsExtensionData; DeclaredPropertyType = typeof(T); JsonTypeInfo propertyTypeInfo = propertyInfo.PropertyTypeInfo; Type declaringType = propertyInfo.DeclaringType; JsonConverter?converter = propertyInfo.Converter; if (converter == null) { converter = propertyTypeInfo.PropertyInfoForTypeInfo.ConverterBase as JsonConverter <T>; if (converter == null) { throw new InvalidOperationException(SR.Format(SR.ConverterForPropertyMustBeValid, declaringType, ClrName, typeof(T))); } } ConverterBase = converter; if (propertyInfo.IgnoreCondition == JsonIgnoreCondition.Always) { IsIgnored = true; Debug.Assert(!ShouldSerialize); Debug.Assert(!ShouldDeserialize); } else { Get = propertyInfo.Getter !; Set = propertyInfo.Setter; HasGetter = Get != null; HasSetter = Set != null; RuntimeTypeInfo = propertyTypeInfo; DeclaringType = declaringType; IgnoreCondition = propertyInfo.IgnoreCondition; MemberType = propertyInfo.IsProperty ? MemberTypes.Property : MemberTypes.Field; _converterIsExternalAndPolymorphic = !ConverterBase.IsInternalConverter && DeclaredPropertyType != ConverterBase.TypeToConvert; PropertyTypeCanBeNull = typeof(T).CanBeNull(); _propertyTypeEqualsTypeToConvert = ConverterBase.TypeToConvert == typeof(T); ConverterStrategy = Converter !.ConverterStrategy; RuntimePropertyType = DeclaredPropertyType; DetermineIgnoreCondition(IgnoreCondition); // TODO: this method needs to also take the number handling option for the declaring type. DetermineNumberHandlingForProperty(propertyInfo.NumberHandling, declaringTypeNumberHandling: null); DetermineSerializationCapabilities(IgnoreCondition); } }
internal string GetPropertyDebugInfo(ReadOnlySpan <byte> unescapedPropertyName) { string propertyName = JsonHelpers.Utf8GetString(unescapedPropertyName); return($"propertyName = {propertyName}; DebugInfo={GetDebugInfo()}"); }
// todo: for readability, refactor this method to split by ClassType(Enumerable, Object, or Value) like Write() private static void ReadCore( JsonSerializerOptions options, ref Utf8JsonReader reader, ref ReadStack state) { while (reader.Read()) { JsonTokenType tokenType = reader.TokenType; if (JsonHelpers.IsInRangeInclusive(tokenType, JsonTokenType.String, JsonTokenType.False)) { Debug.Assert(tokenType == JsonTokenType.String || tokenType == JsonTokenType.Number || tokenType == JsonTokenType.True || tokenType == JsonTokenType.False); if (HandleValue(tokenType, options, ref reader, ref state)) { return; } } else if (tokenType == JsonTokenType.PropertyName) { if (!state.Current.Drain) { Debug.Assert(state.Current.ReturnValue != default); Debug.Assert(state.Current.JsonClassInfo != default); if (state.Current.IsDictionary) { string keyName = reader.GetString(); if (options.DictionaryKeyPolicy != null) { keyName = options.DictionaryKeyPolicy.ConvertName(keyName); } state.Current.JsonPropertyInfo = state.Current.JsonClassInfo.GetPolicyProperty(); state.Current.KeyName = keyName; } else { ReadOnlySpan <byte> propertyName = reader.HasValueSequence ? reader.ValueSequence.ToArray() : reader.ValueSpan; if (reader._stringHasEscaping) { int idx = propertyName.IndexOf(JsonConstants.BackSlash); Debug.Assert(idx != -1); propertyName = GetUnescapedString(propertyName, idx); } state.Current.JsonPropertyInfo = state.Current.JsonClassInfo.GetProperty(options, propertyName, ref state.Current); if (state.Current.JsonPropertyInfo == null) { state.Current.JsonPropertyInfo = s_missingProperty; } state.Current.PropertyIndex++; } } } else if (tokenType == JsonTokenType.StartObject) { HandleStartObject(options, ref reader, ref state); } else if (tokenType == JsonTokenType.EndObject) { if (HandleEndObject(options, ref state)) { return; } } else if (tokenType == JsonTokenType.StartArray) { HandleStartArray(options, ref reader, ref state); } else if (tokenType == JsonTokenType.EndArray) { if (HandleEndArray(options, ref state)) { return; } } else if (tokenType == JsonTokenType.Null) { if (HandleNull(ref reader, ref state, options)) { return; } } } return; }
public IHtmlContent ToJS(object?obj, bool lowerCase = true, bool dateTimeConverter = false) => JsonHelpers.ToJS(obj, lowerCase, dateTimeConverter);
public override void SanitizeMessage(JObject message) { message.Remove("iKey"); JsonHelpers.FlattenNestedObjects(message); }
public void TransformJson() { string result = JsonHelpers.Transform(_baseJson, _xslt); Assert.AreEqual(_serversJson, result); }
// GET api/<controller>/5 public string Get(int id) { ModelSupplierInvoice data = SupplierInvoiceDAO.GetById(id); return(JsonHelpers.stringify(data)); }
protected override IEnumerable <JsonCompletionEntry> GetEntries(JsonCompletionContext context) { MemberNode member = context.ContextNode.FindType <MemberNode>(); if (member == null || member.UnquotedNameText != "files") { yield break; } var parent = member.Parent as ObjectNode; if (!JsonHelpers.TryGetInstallationState(parent, out ILibraryInstallationState state)) { yield break; } if (string.IsNullOrEmpty(state.Name)) { yield break; } IDependencies dependencies = _dependenciesFactory.FromConfigFile(ConfigFilePath); IProvider provider = dependencies.GetProvider(state.ProviderId); ILibraryCatalog catalog = provider?.GetCatalog(); if (catalog == null) { yield break; } Task <ILibrary> task = catalog.GetLibraryAsync(state.Name, state.Version, CancellationToken.None); FrameworkElement presenter = GetPresenter(context); IEnumerable <string> usedFiles = GetUsedFiles(context); if (task.IsCompleted) { if (!(task.Result is ILibrary library)) { yield break; } foreach (string file in library.Files.Keys) { if (!usedFiles.Contains(file)) { ImageSource glyph = WpfUtil.GetIconForFile(presenter, file, out bool isThemeIcon); yield return(new SimpleCompletionEntry(file, glyph, context.Session)); } } } else { yield return(new SimpleCompletionEntry(Resources.Text.Loading, string.Empty, KnownMonikers.Loading, context.Session)); _ = task.ContinueWith(async(t) => { await VisualStudio.Shell.ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); if (!(t.Result is ILibrary library)) { return; } if (!context.Session.IsDismissed) { var results = new List <JsonCompletionEntry>(); foreach (string file in library.Files.Keys) { if (!usedFiles.Contains(file)) { ImageSource glyph = WpfUtil.GetIconForFile(presenter, file, out bool isThemeIcon); results.Add(new SimpleCompletionEntry(file, glyph, context.Session)); } } UpdateListEntriesSync(context, results); } }, TaskScheduler.Default); } }
private async Task RetriggerAsync(bool force) { _lastTyped = DateTime.Now; int delay = force ? 50 : _delay; // Don't leave "stale" completion session up while the user is typing, or else we could // get completion from a stale session. ICompletionSession completionSession = _broker.GetSessions(_textView).FirstOrDefault(); if (completionSession != null && completionSession.Properties.TryGetProperty <bool>(RetriggerCompletion, out bool retrigger) && retrigger) { completionSession.Dismiss(); } await System.Threading.Tasks.Task.Delay(delay); // Prevents retriggering from happening while typing fast if (_lastTyped.AddMilliseconds(delay) > DateTime.Now) { return; } // Completion may have gotten invoked via by Web Editor OnPostTypeChar(). Don't invoke again, or else we get flikering completion list // TODO:Review the design here post-preview 4 and make sure this completion controller doesn't clash with Web Editors Json completion controller completionSession = _broker.GetSessions(_textView).FirstOrDefault(); if (completionSession != null && completionSession.Properties.TryGetProperty <bool>(RetriggerCompletion, out retrigger)) { return; } var doc = JsonEditorDocument.FromTextBuffer(_textView.TextDataModel.DocumentBuffer); if (doc == null) { return; } Node node = JsonHelpers.GetNodeBeforePosition(_textView.Caret.Position.BufferPosition, doc.DocumentNode); if (node == null) { return; } MemberNode memberNode = node.FindType <MemberNode>(); if (memberNode == null || (!memberNode.UnquotedNameText.Equals(ManifestConstants.Library, StringComparison.Ordinal) && !memberNode.UnquotedNameText.Equals(ManifestConstants.Destination, StringComparison.Ordinal) && memberNode.UnquotedValueText?.Length <= 1)) { return; } ObjectNode parent = node.FindType <ObjectNode>(); if (JsonHelpers.TryGetInstallationState(parent, out ILibraryInstallationState state)) { VsHelpers.DTE.ExecuteCommand("Edit.ListMembers"); } }
public MemberStructDef(DefinitionManager manager, JObject src) : base(manager, src) { sortKeyIndices = JsonHelpers.List <int>(src, "sortKey"); unordered = src.Value <bool>("unordered"); }
public static IHtmlString ToJS(string str) { return(new HtmlString(JsonHelpers.Serialize(str))); }
internal JsonParameterInfo?GetParameter( ReadOnlySpan <byte> propertyName, ref ReadStackFrame frame, out byte[] utf8PropertyName) { ParameterRef parameterRef; ulong key = GetKey(propertyName); // Keep a local copy of the cache in case it changes by another thread. ParameterRef[]? localParameterRefsSorted = _parameterRefsSorted; // If there is an existing cache, then use it. if (localParameterRefsSorted != null) { // Start with the current parameter index, and then go forwards\backwards. int parameterIndex = frame.CtorArgumentState !.ParameterIndex; int count = localParameterRefsSorted.Length; int iForward = Math.Min(parameterIndex, count); int iBackward = iForward - 1; while (true) { if (iForward < count) { parameterRef = localParameterRefsSorted[iForward]; if (IsParameterRefEqual(parameterRef, propertyName, key)) { utf8PropertyName = parameterRef.NameFromJson; return(parameterRef.Info); } ++iForward; if (iBackward >= 0) { parameterRef = localParameterRefsSorted[iBackward]; if (IsParameterRefEqual(parameterRef, propertyName, key)) { utf8PropertyName = parameterRef.NameFromJson; return(parameterRef.Info); } --iBackward; } } else if (iBackward >= 0) { parameterRef = localParameterRefsSorted[iBackward]; if (IsParameterRefEqual(parameterRef, propertyName, key)) { utf8PropertyName = parameterRef.NameFromJson; return(parameterRef.Info); } --iBackward; } else { // Property was not found. break; } } } // No cached item was found. Try the main dictionary which has all of the parameters. Debug.Assert(ParameterCache != null); if (ParameterCache.TryGetValue(JsonHelpers.Utf8GetString(propertyName), out JsonParameterInfo? info)) { Debug.Assert(info != null); if (Options.PropertyNameCaseInsensitive) { if (propertyName.SequenceEqual(info.NameAsUtf8Bytes)) { Debug.Assert(key == GetKey(info.NameAsUtf8Bytes.AsSpan())); // Use the existing byte[] reference instead of creating another one. utf8PropertyName = info.NameAsUtf8Bytes !; } else { // Make a copy of the original Span. utf8PropertyName = propertyName.ToArray(); } } else { Debug.Assert(key == GetKey(info.NameAsUtf8Bytes !.AsSpan())); utf8PropertyName = info.NameAsUtf8Bytes !; } } else { Debug.Assert(info == null); // Make a copy of the original Span. utf8PropertyName = propertyName.ToArray(); } // Check if we should add this to the cache. // Only cache up to a threshold length and then just use the dictionary when an item is not found in the cache. int cacheCount = 0; if (localParameterRefsSorted != null) { cacheCount = localParameterRefsSorted.Length; } // Do a quick check for the stable (after warm-up) case. if (cacheCount < ParameterNameCountCacheThreshold) { // Do a slower check for the warm-up case. if (frame.CtorArgumentState !.ParameterRefCache != null) { cacheCount += frame.CtorArgumentState.ParameterRefCache.Count; } // Check again to append the cache up to the threshold. if (cacheCount < ParameterNameCountCacheThreshold) { if (frame.CtorArgumentState.ParameterRefCache == null) { frame.CtorArgumentState.ParameterRefCache = new List <ParameterRef>(); } parameterRef = new ParameterRef(key, info !, utf8PropertyName); frame.CtorArgumentState.ParameterRefCache.Add(parameterRef); } } return(info); }
internal JsonPropertyInfo GetProperty(JsonSerializerOptions options, ReadOnlySpan <byte> propertyName, ref ReadStackFrame frame) { // If we should compare with case-insensitive, normalize to an uppercase format since that is what is cached on the propertyInfo. if (options.PropertyNameCaseInsensitive) { string utf16PropertyName = JsonHelpers.Utf8GetString(propertyName); string upper = utf16PropertyName.ToUpperInvariant(); propertyName = Encoding.UTF8.GetBytes(upper); } ulong key = GetKey(propertyName); JsonPropertyInfo info = null; // First try sorted lookup. int propertyIndex = frame.PropertyIndex; // If we're not trying to build the cache locally, and there is an existing cache, then use it. bool hasPropertyCache = frame.PropertyRefCache == null && _propertyRefsSorted != null; if (hasPropertyCache) { // This .Length is consistent no matter what json data intialized _propertyRefsSorted. int count = _propertyRefsSorted.Length; if (count != 0) { int iForward = propertyIndex; int iBackward = propertyIndex - 1; while (iForward < count || iBackward >= 0) { if (iForward < count) { if (TryIsPropertyRefEqual(ref _propertyRefsSorted[iForward], propertyName, key, ref info)) { return(info); } ++iForward; } if (iBackward >= 0) { if (TryIsPropertyRefEqual(ref _propertyRefsSorted[iBackward], propertyName, key, ref info)) { return(info); } --iBackward; } } } } // Try the main list which has all of the properties in a consistent order. // We could get here even when hasPropertyCache==true if there is a race condition with different json // property ordering and _propertyRefsSorted is re-assigned while in the loop above. for (int i = 0; i < _propertyRefs.Count; i++) { PropertyRef propertyRef = _propertyRefs[i]; if (TryIsPropertyRefEqual(ref propertyRef, propertyName, key, ref info)) { break; } } if (!hasPropertyCache) { if (propertyIndex == 0 && frame.PropertyRefCache == null) { // Create the temporary list on first property access to prevent a partially filled List. frame.PropertyRefCache = new List <PropertyRef>(); } if (info != null) { Debug.Assert(frame.PropertyRefCache != null); frame.PropertyRefCache.Add(new PropertyRef(key, info)); } } return(info); }
public async Task DoWorkAsync_ShouldReturnAFailureEventandLog_WhenMissingBlob() { // Arrange var topicEndpointUri = new Uri("https://www.topichost.com"); var appInsightsUri = new Uri("https://www.appinsights.com"); var opCtx = JsonHelpers.JsonToJObject("{\"someIdType1\":\"someValue1\", \"someIdType2\":\"someValue2\"}"); var testEvent = new EventGridEvent { EventTime = DateTime.UtcNow, EventType = CustomEventTypes.RequestBlobDelete, DataVersion = "1.0", Data = JsonConvert.SerializeObject(new RequestBlobDeleteDTO { BlobUri = new Uri(_expectedInboxUrl), OperationContext = opCtx, }), }; var expectedBlobMetadata = JsonHelpers.JsonToJObject("{\"someProp1\":\"someValue1\", \"someProp2\":\"someValue2\"}", true); EventGridEvent publishedEvent = null; // Arrange Mocks Mock.Get(_settingsProvider) .Setup(x => x.GetAppSettingsValue(Publishing.TopicOutboundEndpointSettingName)) .Returns(topicEndpointUri.ToString()); Mock.Get(_storageService) .Setup(x => x.GetBlobMetadataAsync(It.IsAny <Uri>(), It.IsAny <StorageClientProviderContext>())) .ReturnsAsync((JObject)null); Mock.Get(_storageService) .Setup(x => x.BlobDelete(It.IsAny <Uri>(), It.IsAny <StorageClientProviderContext>())) .ReturnsAsync(false); Mock.Get(_eventGridPublisher) .Setup(x => x.PublishEventToTopic(It.IsAny <EventGridEvent>())) .Callback <EventGridEvent>((eventGridEvent) => publishedEvent = eventGridEvent) .ReturnsAsync(true); Mock.Get(_logger) .Setup(x => x.LogEventObject( out appInsightsUri, LogEventIds.NoSuchBlobInBlobDeleteHandler, It.IsAny <object>())); Mock.Get(_logger) .Setup(x => x.LogExceptionObject( out appInsightsUri, LogEventIds.GridwichUnhandledException, It.IsAny <Exception>(), It.IsAny <object>())); // Act: var handleAsyncResult = await _handler.HandleAsync(testEvent).ConfigureAwait(true); // Assert: handleAsyncResult.ShouldBe(true, "handleAsync should always return true"); Mock.Get(_logger).Verify(x => x.LogEventObject(out appInsightsUri, LogEventIds.NoSuchBlobInBlobDeleteHandler, It.IsAny <object>()), Times.Once, "A warning should be logged when the blob is missing [1]"); Mock.Get(_storageService).Verify(x => x.GetBlobMetadataAsync(It.IsAny <Uri>(), It.IsAny <StorageClientProviderContext>()), Times.Once, "Should attempt to get blob metadata."); Mock.Get(_storageService).Verify(x => x.BlobDelete(It.IsAny <Uri>(), It.IsAny <StorageClientProviderContext>()), Times.Once, "Should attempt to delete the blob."); Mock.Get(_eventGridPublisher) .Verify(x => x.PublishEventToTopic(It.IsAny <EventGridEvent>()), Times.Exactly(2), "Should only publish ACK + Failure."); // Assert publishedEvent: publishedEvent.EventType.ShouldBe(CustomEventTypes.ResponseFailure); publishedEvent.EventTime.ShouldBeInRange(testEvent.EventTime, testEvent.EventTime.AddMinutes(1)); publishedEvent.Data.ShouldBeOfType(typeof(ResponseFailureDTO)); var data = (ResponseFailureDTO)publishedEvent.Data; data.ShouldNotBeNull(); data.OperationContext.ContainsKey("someIdType1").ShouldBeTrue(); data.OperationContext["someIdType1"].ShouldBe("someValue1"); data.OperationContext.ContainsKey("someIdType2").ShouldBeTrue(); data.OperationContext["someIdType2"].ShouldBe("someValue2"); data.HandlerId.ShouldBe(_handler.GetHandlerId(), StringCompareShould.IgnoreCase); // TODO: likely needs adjustment after refactoring of failure events }
public void VqResult_01() { var result = "{\"took\":31,\"timed_out\":false,\"_shards\":{\"total\":3,\"successful\":3,\"skipped\":0,\"failed\":0},\"hits\":{\"total\":50888,\"max_score\":0,\"hits\":[]},\"aggregations\":" + "{\"agg_filter_1\":{\"meta\":{},\"doc_count\":100,\"EventTypesSub\":{\"doc_count_error_upper_bound\":0,\"sum_other_doc_count\":0,\"buckets\":[{\"key\":\"UnderVoltage\"," + "\"doc_count\":96,\"Cardinality\":{\"value\":65}},{\"key\":\"OverVoltage\",\"doc_count\":4,\"Cardinality\":{\"value\":3}}]},\"WorkOrderStateSub\":{\"doc_count_error_upper_bound\"" + ":0,\"sum_other_doc_count\":0,\"buckets\":[{\"key\":\"New\",\"doc_count\":100}]}},\"cardinality_Asset.AssetGuid\":{\"value\":23062},\"agg_filter_0\":{\"meta\":{},\"doc_count\":547," + "\"EventTypesAmi\":{\"doc_count_error_upper_bound\":0,\"sum_other_doc_count\":0,\"buckets\":[{\"key\":\"UnderVoltage\",\"doc_count\":506,\"Cardinality\":{\"value\":506}},{\"key\":" + "\"OverVoltage\",\"doc_count\":41,\"Cardinality\":{\"value\":41}}]},\"WorkOrderStateAmi\":{\"doc_count_error_upper_bound\":0,\"sum_other_doc_count\":0,\"buckets\":[{\"key\":\"New\"," + "\"doc_count\":547}]}}}}"; var obj = JsonConvert.DeserializeObject <DocumentSearchResult <VoltageQualityEvent> >(result, JsonHelpers.CreateSerializerSettings()); }
internal JsonPropertyInfo GetProperty( ReadOnlySpan <byte> propertyName, ref ReadStackFrame frame, out byte[] utf8PropertyName) { PropertyRef propertyRef; ValidateCanBeUsedForDeserialization(); ulong key = GetKey(propertyName); // Keep a local copy of the cache in case it changes by another thread. PropertyRef[]? localPropertyRefsSorted = _propertyRefsSorted; // If there is an existing cache, then use it. if (localPropertyRefsSorted != null) { // Start with the current property index, and then go forwards\backwards. int propertyIndex = frame.PropertyIndex; int count = localPropertyRefsSorted.Length; int iForward = Math.Min(propertyIndex, count); int iBackward = iForward - 1; while (true) { if (iForward < count) { propertyRef = localPropertyRefsSorted[iForward]; if (IsPropertyRefEqual(propertyRef, propertyName, key)) { utf8PropertyName = propertyRef.NameFromJson; return(propertyRef.Info); } ++iForward; if (iBackward >= 0) { propertyRef = localPropertyRefsSorted[iBackward]; if (IsPropertyRefEqual(propertyRef, propertyName, key)) { utf8PropertyName = propertyRef.NameFromJson; return(propertyRef.Info); } --iBackward; } } else if (iBackward >= 0) { propertyRef = localPropertyRefsSorted[iBackward]; if (IsPropertyRefEqual(propertyRef, propertyName, key)) { utf8PropertyName = propertyRef.NameFromJson; return(propertyRef.Info); } --iBackward; } else { // Property was not found. break; } } } // No cached item was found. Try the main dictionary which has all of the properties. #if DEBUG if (PropertyCache == null) { Debug.Fail($"Property cache is null. {GetPropertyDebugInfo(propertyName)}"); } #endif if (PropertyCache !.TryGetValue(JsonHelpers.Utf8GetString(propertyName), out JsonPropertyInfo? info)) { Debug.Assert(info != null, "PropertyCache contains null JsonPropertyInfo"); if (Options.PropertyNameCaseInsensitive) { if (propertyName.SequenceEqual(info.NameAsUtf8Bytes)) { #if DEBUG ulong recomputedKey = GetKey(info.NameAsUtf8Bytes.AsSpan()); if (key != recomputedKey) { string propertyNameStr = JsonHelpers.Utf8GetString(propertyName); Debug.Fail($"key {key} [propertyName={propertyNameStr}] does not match re-computed value {recomputedKey} for the same sequence (case-insensitive). {info.GetDebugInfo()}"); } #endif // Use the existing byte[] reference instead of creating another one. utf8PropertyName = info.NameAsUtf8Bytes !; } else { // Make a copy of the original Span. utf8PropertyName = propertyName.ToArray(); } } else { #if DEBUG ulong recomputedKey = GetKey(info.NameAsUtf8Bytes.AsSpan()); if (key != recomputedKey) { string propertyNameStr = JsonHelpers.Utf8GetString(propertyName); Debug.Fail($"key {key} [propertyName={propertyNameStr}] does not match re-computed value {recomputedKey} for the same sequence (case-sensitive). {info.GetDebugInfo()}"); } #endif utf8PropertyName = info.NameAsUtf8Bytes; } }
/// <summary> /// Parse callback eevent data from JSON /// </summary> public static CallbackEvent[] CreateFromJson(string json) => JsonConvert.DeserializeObject <CallbackEvent[]>(json, JsonHelpers.GetSerializerSettings()).Select(SetReplyTo).ToArray();
public IHtmlContent ToJS(string?str, bool lowerCase = true, bool dateTimeConverter = false) => JsonHelpers.ToJS(str, lowerCase, dateTimeConverter);
private static void ReadCore( JsonSerializerOptions options, ref Utf8JsonReader reader, ref ReadStack state) { try { while (reader.Read()) { JsonTokenType tokenType = reader.TokenType; if (JsonHelpers.IsInRangeInclusive(tokenType, JsonTokenType.String, JsonTokenType.False)) { Debug.Assert(tokenType == JsonTokenType.String || tokenType == JsonTokenType.Number || tokenType == JsonTokenType.True || tokenType == JsonTokenType.False); if (HandleValue(tokenType, options, ref reader, ref state)) { continue; } } else if (tokenType == JsonTokenType.PropertyName) { HandlePropertyName(options, ref reader, ref state); } else if (tokenType == JsonTokenType.StartObject) { if (state.Current.SkipProperty) { state.Push(); state.Current.Drain = true; } else if (state.Current.IsProcessingValue) { if (HandleValue(tokenType, options, ref reader, ref state)) { continue; } } else if (state.Current.IsProcessingDictionary) { HandleStartDictionary(options, ref reader, ref state); } else { HandleStartObject(options, ref reader, ref state); } } else if (tokenType == JsonTokenType.EndObject) { if (state.Current.Drain) { state.Pop(); } else if (state.Current.IsProcessingDictionary) { HandleEndDictionary(options, ref reader, ref state); } else { HandleEndObject(options, ref reader, ref state); } } else if (tokenType == JsonTokenType.StartArray) { if (!state.Current.IsProcessingValue) { HandleStartArray(options, ref reader, ref state); } else if (HandleValue(tokenType, options, ref reader, ref state)) { continue; } } else if (tokenType == JsonTokenType.EndArray) { if (HandleEndArray(options, ref reader, ref state)) { continue; } } else if (tokenType == JsonTokenType.Null) { if (HandleNull(ref reader, ref state, options)) { continue; } } } } catch (JsonReaderException e) { // Re-throw with Path information. ThrowHelper.ReThrowWithPath(e, state.PropertyPath); } return; }