public static string SerializeDB(SerializeOptions options, bool includeTabularEditorTag) { var db = TabularModelHandler.Singleton.Database; if (includeTabularEditorTag) { db.AddTabularEditorTag(); } else { db.RemoveTabularEditorTag(); } // Remove object translations with no objects assigned: var nullTrans = db.Model.Cultures.SelectMany(c => c.ObjectTranslations).Where(ot => ot.Object == null).ToList(); if (nullTrans.Count > 0) { foreach (var ot in nullTrans) { ot.Culture.ObjectTranslations.Remove(ot); } } var serializedDB = TOM.JsonSerializer.SerializeDatabase(db, new TOM.SerializeOptions() { IgnoreInferredObjects = options.IgnoreInferredObjects, IgnoreTimestamps = options.IgnoreTimestamps, IgnoreInferredProperties = options.IgnoreInferredProperties, SplitMultilineStrings = options.SplitMultilineStrings, IncludeRestrictedInformation = db.Model.Annotations.Contains(ANN_SAVESENSITIVE) && db.Model.Annotations[ANN_SAVESENSITIVE].Value == "1" }); db.RemoveTabularEditorTag(); // Hack: Remove \r characters from multiline strings in the BIM: // "1 + 2\r", -> "1 + 2", if (options.SplitMultilineStrings) { serializedDB = serializedDB.Replace("\\r\",\r\n", "\",\r\n"); } return(serializedDB); }
public bool Equals(SerializeOptions other) { if (ReferenceEquals(this, other)) { return(true); } if (ReferenceEquals(null, other)) { return(false); } return(other.Levels.SetEquals(this.Levels) && other.IgnoreInferredObjects == IgnoreInferredObjects && other.IgnoreInferredProperties == IgnoreInferredProperties && other.IgnoreTimestamps == IgnoreTimestamps && other.SplitMultilineStrings == SplitMultilineStrings && other.PrefixFilenames == PrefixFilenames && other.LocalTranslations == LocalTranslations && other.LocalPerspectives == LocalPerspectives && other.LocalRelationships == LocalRelationships); }
public static string SerializeDB(SerializeOptions options) { var db = TabularModelHandler.Singleton.Database; var serializedDB = TOM.JsonSerializer.SerializeDatabase(db, new TOM.SerializeOptions() { IgnoreInferredObjects = options.IgnoreInferredObjects, IgnoreTimestamps = options.IgnoreTimestamps, IgnoreInferredProperties = options.IgnoreInferredProperties, SplitMultilineStrings = options.SplitMultilineStrings, IncludeRestrictedInformation = db.Model.Annotations.Contains(ANN_SAVESENSITIVE) && db.Model.Annotations[ANN_SAVESENSITIVE].Value == "1" }); // Hack: Remove \r characters from multiline strings in the BIM: // "1 + 2\r", -> "1 + 2", if (options.SplitMultilineStrings) { serializedDB = serializedDB.Replace("\\r\",\r\n", "\",\r\n"); } return(serializedDB); }
/// <summary> /// Saves the model to the specified folder using the specified serialize options. /// </summary> public void SaveToFolder(Model model, string path, SerializeOptions options) { if (options.LocalTranslations) { model.StoreTranslationsAsAnnotations(); } if (options.LocalPerspectives) { model.StorePerspectivesToAnnotations(); } if (options.LocalRelationships) { model.StoreRelationshipsAsAnnotations(); } var json = Serializer.SerializeDB(options); var jobj = JObject.Parse(json); jobj["name"] = model.Database?.Name ?? "SemanticModel"; if (model.Database != null) { if (!model.Database.Name.EqualsI(model.Database.ID)) { jobj["id"] = model.Database.ID; } else if (jobj["id"] != null) { jobj["id"].Remove(); } } var jModel = jobj["model"] as JObject; var dataSources = options.Levels.Contains("Data Sources") ? PopArray(jModel, "dataSources") : null; var tables = options.Levels.Contains("Tables") ? PopArray(jModel, "tables") : null; var relationships = options.Levels.Contains("Relationships") || options.LocalRelationships ? PopArray(jModel, "relationships") : null; var cultures = options.Levels.Contains("Translations") || options.LocalTranslations ? PopArray(jModel, "cultures") : null; var perspectives = options.Levels.Contains("Perspectives") || options.LocalPerspectives ? PopArray(jModel, "perspectives") : null; var roles = options.Levels.Contains("Roles") ? PopArray(jModel, "roles") : null; WriteIfChanged(path + "\\database.json", jobj.ToString(Formatting.Indented)); if (relationships != null && !options.LocalRelationships) { OutArray(path, "relationships", relationships, options); } if (perspectives != null && !options.LocalPerspectives) { OutArray(path, "perspectives", perspectives, options); } if (cultures != null && !options.LocalTranslations) { OutArray(path, "cultures", cultures, options); } if (dataSources != null) { OutArray(path, "dataSources", dataSources, options); } if (roles != null) { OutArray(path, "roles", roles, options); } if (tables != null) { int n = 0; foreach (JObject t in tables) { var table = model.Tables[t["name"].ToString()]; var columns = options.Levels.Contains("Tables/Columns") ? PopArray(t, "columns") : null; var partitions = options.Levels.Contains("Tables/Partitions") ? PopArray(t, "partitions") : null; var measures = options.Levels.Contains("Tables/Measures") ? PopArray(t, "measures") : null; var hierarchies = options.Levels.Contains("Tables/Hierarchies") ? PopArray(t, "hierarchies") : null; var annotations = options.Levels.Contains("Tables/Annotations") ? PopArray(t, "annotations") : null; var calculationItems = options.Levels.Contains("Tables/Calculation Items") ? PopArray(t, "calculationGroup.calculationItems") : null; var tableName = Sanitize(table.Name); var tablePath = path + "\\tables\\" + (options.PrefixFilenames ? n.ToString("D3") + " " : "") + tableName; var p = tablePath + "\\" + tableName + ".json"; var fi = new FileInfo(p); if (!fi.Directory.Exists) { fi.Directory.Create(); } WriteIfChanged(p, t.ToString(Formatting.Indented)); if (measures != null) { OutArray(tablePath, "measures", measures, options); } if (columns != null) { OutArray(tablePath, "columns", columns, options); } if (hierarchies != null) { OutArray(tablePath, "hierarchies", hierarchies, options); } if (partitions != null) { OutArray(tablePath, "partitions", partitions, options); } if (annotations != null) { OutArray(tablePath, "annotations", annotations, options); } if (calculationItems != null) { OutArray(tablePath, "calculationItems", calculationItems, options); } n++; } } RemoveUnusedFiles(path + "\\tables", CurrentFiles); RemoveUnusedFiles(path + "\\relationships", CurrentFiles); RemoveUnusedFiles(path + "\\perspectives", CurrentFiles); RemoveUnusedFiles(path + "\\cultures", CurrentFiles); RemoveUnusedFiles(path + "\\dataSources", CurrentFiles); RemoveUnusedFiles(path + "\\roles", CurrentFiles); }
public static void SaveToFolder(this Model model, string path, SerializeOptions options) { var serializer = new FileWriter(); serializer.SaveToFolder(model, path, options); }
public static string SerializeDB(SerializeOptions options, bool includeTabularEditorTag) { var db = TabularModelHandler.Singleton.Database; if (includeTabularEditorTag) { db.AddTabularEditorTag(); } else { db.RemoveTabularEditorTag(); } // Remove object translations with no objects assigned: var nullTrans = db.Model.Cultures.SelectMany(c => c.ObjectTranslations).Where(ot => ot.Object == null).ToList(); if (nullTrans.Count > 0) { foreach (var ot in nullTrans) { ot.Culture.ObjectTranslations.Remove(ot); } } var serializeOptions = new TOM.SerializeOptions() { IgnoreInferredObjects = options.IgnoreInferredObjects, IgnoreTimestamps = options.IgnoreTimestamps, IgnoreInferredProperties = options.IgnoreInferredProperties, SplitMultilineStrings = options.SplitMultilineStrings, IncludeRestrictedInformation = db.Model.Annotations.Contains(ANN_SAVESENSITIVE) && db.Model.Annotations[ANN_SAVESENSITIVE].Value == "1" }; var serializedDB = TOM.JsonSerializer.SerializeDatabase(db, serializeOptions); // Hack: Remove \r characters from multiline strings in the BIM: // "1 + 2\r", -> "1 + 2", if (options.SplitMultilineStrings) { serializedDB = serializedDB.Replace("\\r\",\r\n", "\",\r\n"); } var jObject = JObject.Parse(serializedDB); if (options.IgnoreLineageTags) { var lineageTags = jObject.Descendants().OfType <JProperty>().Where(p => p.Name == "lineageTag").ToList(); foreach (var lineageTag in lineageTags) { lineageTag.Remove(); } } // Remove privacy settings if specified: foreach (var obj in jObject.DescendantsAndSelf().OfType <JObject>().ToList()) { if (options.IgnorePrivacySettings) { if (obj.ContainsKey("PrivacySetting")) { obj.Remove("PrivacySetting"); } } } if (!serializeOptions.IncludeRestrictedInformation) { // Remove sensitive properties entirely (since TOM.JsonSerializer.SerializeDatabase would have asterisk'ed their values) foreach (var jDataSource in jObject.SelectTokens("model.dataSources[*]").OfType <JObject>()) { foreach (var jSensitive in jDataSource.Descendants().OfType <JProperty>().Where(p => p.Name.IsOneOf("key", "password", "pwd", "secret")).ToList()) { jSensitive.Remove(); } } } serializedDB = jObject.ToString(); return(serializedDB); }