/// <summary> /// Serializes originalDataset to JSON, deserializes, and re-serializes. /// Verifies that both datasets are equal, and both json serializations are equal! /// </summary> private void VerifyJsonTripleTrip(DicomDataset originalDataset, bool expectFail = false) { string json = DicomTypeTranslater.SerializeDatasetToJson(originalDataset, _jsonDicomConverter); _logger.Debug($"Initial json:\n{json}"); DicomDataset recoDataset = DicomTypeTranslater.DeserializeJsonToDataset(json, _jsonDicomConverter); string json2 = DicomTypeTranslater.SerializeDatasetToJson(recoDataset, _jsonDicomConverter); _logger.Debug($"Final json:\n{json}"); if (expectFail) { Assert.AreNotEqual(json, json2); } else { Assert.AreEqual(json, json2); } // NOTE: Group length elements have been retired from the standard, and have never been included in any JSON conversion. // Remove them here to allow comparison between datasets. originalDataset.RemoveGroupLengths(); if (expectFail) { Assert.False(DicomDatasetHelpers.ValueEquals(originalDataset, recoDataset)); } else { Assert.True(DicomDatasetHelpers.ValueEquals(originalDataset, recoDataset)); } }
private void Validate(SeriesMessage message, BsonDocument document) { Assert.False(message == null || document == null); BsonElement element; Assert.True(document.TryGetElement("header", out element)); var docHeader = (BsonDocument)element.Value; Assert.AreEqual(_seriesMessageProps.Count - 3, docHeader.ElementCount); Assert.AreEqual(message.DirectoryPath, docHeader["DirectoryPath"].AsString); Assert.AreEqual(message.NationalPACSAccessionNumber, docHeader["NationalPACSAccessionNumber"].AsString); Assert.AreEqual(message.ImagesInSeries, docHeader["ImagesInSeries"].AsInt32); DicomDataset dataset = DicomTypeTranslater.DeserializeJsonToDataset(message.DicomDataset); Assert.NotNull(dataset); BsonDocument datasetDocument = DicomTypeTranslaterReader.BuildBsonDocument(dataset); document.Remove("_id"); document.Remove("header"); Assert.AreEqual(datasetDocument, document); }
public void TestBlankAndNullSerialization() { var dataset = new DicomDataset { new DicomDecimalString(DicomTag.SelectorDSValue, default(string)), new DicomIntegerString(DicomTag.SelectorISValue, ""), new DicomFloatingPointSingle(DicomTag.SelectorFLValue, default(float)), new DicomFloatingPointDouble(DicomTag.SelectorFDValue) }; string json = DicomTypeTranslater.SerializeDatasetToJson(dataset, _jsonDicomConverter); _logger.Debug(json); DicomDataset recoDataset = DicomTypeTranslater.DeserializeJsonToDataset(json, _jsonDicomConverter); Assert.True(DicomDatasetHelpers.ValueEquals(dataset, recoDataset)); }
protected override void ProcessMessageImpl(IMessageHeader header, DicomFileMessage message, ulong tag) { DicomDataset dataset; try { dataset = DicomTypeTranslater.DeserializeJsonToDataset(message.DicomDataset); } catch (Exception e) { ErrorAndNack(header, tag, "Could not rebuild DicomDataset from message", e); return; } var toQueue = new QueuedImage(header, tag, message, dataset); lock (_oQueueLock) _imageQueue.Enqueue(toQueue); }
public void BsonRoundTrip_MaskedTags_ConvertedCorrectly() { const string rawJson = "{\"60000010\":{\"vr\":\"US\",\"val\":[128]},\"60000011\":{\"vr\":\"US\",\"val\":[614]},\"60000040\":" + "{\"vr\":\"CS\",\"val\":\"G\"},\"60000050\":{\"vr\":\"SS\",\"val\":[0,0]},\"60000100\":{\"vr\":\"US\",\"val\":[1]}," + "\"60000102\":{\"vr\":\"US\",\"val\":[0]},\"60020010\":{\"vr\":\"US\",\"val\":[512]},\"60020011\":{\"vr\":\"US\",\"val\":[613]}," + "\"60020040\":{\"vr\":\"CS\",\"val\":\"H\"},\"60020050\":{\"vr\":\"SS\",\"val\":[1,2]},\"60020100\":{\"vr\":\"US\",\"val\":[3]}," + "\"60020102\":{\"vr\":\"US\",\"val\":[4]}}"; DicomDataset maskDataset = DicomTypeTranslater.DeserializeJsonToDataset(rawJson); Assert.AreEqual(12, maskDataset.Count()); foreach (DicomItem item in maskDataset.Where(x => x.Tag.DictionaryEntry.Keyword == DicomTag.OverlayRows.DictionaryEntry.Keyword)) { _logger.Debug("{0} {1} - Val: {2}", item.Tag, item.Tag.DictionaryEntry.Keyword, maskDataset.GetString(item.Tag)); } VerifyBsonTripleTrip(maskDataset); }
public void TestMaskedTagSerialization() { //Example: OverlayRows element has a masked tag of (60xx,0010) _logger.Info("DicomTag.OverlayRows.DictionaryEntry.MaskTag: " + DicomTag.OverlayRows.DictionaryEntry.MaskTag); const string rawJson = "{\"60000010\":{\"vr\":\"US\",\"val\":[128]},\"60000011\":{\"vr\":\"US\",\"val\":[614]},\"60000040\":" + "{\"vr\":\"CS\",\"val\":\"G\"},\"60000050\":{\"vr\":\"SS\",\"val\":[0,0]},\"60000100\":{\"vr\":\"US\"," + "\"val\":[1]},\"60000102\":{\"vr\":\"US\",\"val\":[0]},\"60020010\":{\"vr\":\"US\",\"val\":[512]}," + "\"60020011\":{\"vr\":\"US\",\"val\":[614]},\"60020040\":{\"vr\":\"CS\",\"val\":\"G\"},\"60020050\":" + "{\"vr\":\"SS\",\"val\":[0,0]},\"60020100\":{\"vr\":\"US\",\"val\":[1]},\"60020102\":{\"vr\":\"US\",\"val\":[0]}}"; DicomDataset maskDataset = DicomTypeTranslater.DeserializeJsonToDataset(rawJson); foreach (DicomItem item in maskDataset.Where(x => x.Tag.DictionaryEntry.Keyword == DicomTag.OverlayRows.DictionaryEntry.Keyword)) { _logger.Debug("{0} {1} - Val: {2}", item.Tag, item.Tag.DictionaryEntry.Keyword, maskDataset.GetString(item.Tag)); } VerifyJsonTripleTrip(maskDataset); }
public void TestIdentifierSwapForGuid(DatabaseType dbType) { var db = GetCleanedServer(dbType); var mapTbl = db.ExpectTable("Map"); //the declaration of what the guid namer table should be var options = new IdentifierMapperOptions(); options.MappingConnectionString = db.Server.Builder.ConnectionString; options.MappingTableName = mapTbl.GetFullyQualifiedName(); options.SwapColumnName = "priv"; options.ReplacementColumnName = "pub"; options.MappingDatabaseType = dbType; var swapper = new ForGuidIdentifierSwapper(); swapper.Setup(options); swapper.Setup(options); swapper.Setup(options); //this isn't just for the lols, this will test both the 'create it mode' and the 'discover it mode' var consumer = new IdentifierMapperQueueConsumer(Mock.Of <IProducerModel>(), swapper); var msg = GetTestDicomFileMessage(); string reason; consumer.SwapIdentifier(msg, out reason); var newDs = DicomTypeTranslater.DeserializeJsonToDataset(msg.DicomDataset); var guidAllocated = newDs.GetValue <string>(DicomTag.PatientID, 0); var dt = mapTbl.GetDataTable(); Assert.AreEqual(1, dt.Rows.Count); //e.g. '841A2E3E-B7C9-410C-A5D1-816B95C0E806' Assert.AreEqual(36, guidAllocated.Length); }
private void Validate(DicomFileMessage message, MessageHeader header, BsonDocument document) { Assert.False(message == null || document == null); BsonElement element; Assert.True(document.TryGetElement("header", out element)); var docHeader = (BsonDocument)element.Value; Assert.AreEqual(_imageMessageProps.Count - 3, docHeader.ElementCount); ValidateHeader(message, header, docHeader); DicomDataset dataset = DicomTypeTranslater.DeserializeJsonToDataset(message.DicomDataset); Assert.NotNull(dataset); BsonDocument datasetDocument = DicomTypeTranslaterReader.BuildBsonDocument(dataset); document.Remove("_id"); document.Remove("header"); Assert.AreEqual(datasetDocument, document); }
public void JsonSerialization_SerializeBinaryFalse_ContainsEmptyTags() { if (_jsonDicomConverter.GetType().Name != "SmiLazyJsonDicomConverter") { Assert.Pass("Only applicable for SmiLazyJsonDicomConverter"); } var ds = new DicomDataset { new DicomOtherByte(DicomTag.SelectorOBValue, byte.MinValue), new DicomOtherWord(DicomTag.SelectorOWValue, byte.MinValue), new DicomUnknown(DicomTag.SelectorUNValue, byte.MinValue) }; DicomTypeTranslater.SerializeBinaryData = false; string json = DicomTypeTranslater.SerializeDatasetToJson(ds); Assert.DoesNotThrow(() => JToken.Parse(json)); DicomDataset recoDs = DicomTypeTranslater.DeserializeJsonToDataset(json); Assert.AreEqual(ds.Count(), recoDs.Count()); AssertBlacklistedNulls(recoDs); }
public override void AddToWriteQueue(SeriesMessage message, IMessageHeader header, ulong deliveryTag) { // Only time we are not processing is if we are shutting down anyway if (IsStopping) { return; } if (Model == null) { throw new ApplicationException("Model needs to be set before messages can be processed"); } DicomDataset dataset; try { dataset = DicomTypeTranslater.DeserializeJsonToDataset(message.DicomDataset); } catch (Exception e) { throw new ApplicationException("Could not deserialize json to dataset", e); } BsonDocument datasetDoc; try { datasetDoc = DicomTypeTranslaterReader.BuildBsonDocument(dataset); } catch (Exception e) { throw new ApplicationException("Exception converting dataset to BsonDocument", e); } BsonDocument bsonHeader = MongoDocumentHeaders.SeriesDocumentHeader(message); BsonDocument document = new BsonDocument() .Add("header", bsonHeader) .AddRange(datasetDoc); int docByteLength = document.ToBson().Length; if (docByteLength > MaxDocumentSize) { throw new ApplicationException($"BsonDocument was larger than the max allowed size (have {docByteLength}, max is {MaxDocumentSize})"); } var forceProcess = false; lock (LockObj) { ToProcess.Enqueue(new Tuple <BsonDocument, ulong>(document, deliveryTag)); if (ToProcess.Count >= MaxQueueSize) { forceProcess = true; } } if (!forceProcess) { return; } Logger.Debug("SeriesMessageProcessor: Max queue size reached, calling ProcessQueue"); ProcessQueue(); }
private void AssertDicomFileMessageHasPatientID(DicomFileMessage msg, string patientId) { var newDs = DicomTypeTranslater.DeserializeJsonToDataset(msg.DicomDataset); Assert.AreEqual(newDs.GetValue <string>(DicomTag.PatientID, 0), patientId); }
public bool SwapIdentifier(DicomFileMessage msg, out string reason) { DicomDataset ds; try { ds = DicomTypeTranslater.DeserializeJsonToDataset(msg.DicomDataset); } catch (Exception e) { throw new ApplicationException("Failed to deserialize dataset", e); } if (!ds.Contains(DicomTag.PatientID)) { reason = "Dataset did not contain PatientID"; return(false); } var from = (string)DicomTypeTranslaterReader.GetCSharpValue(ds, DicomTag.PatientID); if (string.IsNullOrWhiteSpace(from)) { reason = "PatientID was blank"; return(false); } string to = _swapper.GetSubstitutionFor(from, out reason); if (to == null) { reason = "Swapper " + _swapper + " returned null"; return(false); } // Update the JSON deserialized dicom dataset ds.AddOrUpdate(DicomTag.PatientID, to); string updatedJson; try { updatedJson = DicomTypeTranslater.SerializeDatasetToJson(ds); } catch (Exception e) { throw new ApplicationException("Failed to serialize dataset", e); } // Unlikely, but should still check if (updatedJson == null) { reason = "Updated json string was null"; return(false); } // Override the message DicomDataset with the new serialized dataset msg.DicomDataset = updatedJson; return(true); }