c#/json.net deserialize child with data from parent - json.net

class Item
{
public string Id;
public List<Item> Children;
}
json
{
"Id" : "/parent",
"Children" : [
{
"Id" : "/child1"
}
]
}
I have class for creating nested items.
How would I have a child item access the parent's Id field during/after deserialization of the child? Or is there a way to send the parent Id field when the child is to be created?
Example:
Parent Id "/parent"
Child Id "/child1"
I want the child Id "/parent/child1"
For reasons, I cannot use '[OnDeserialized]' after the parent is finished to loop children and change Id. Nor do I have access to change the json files themselves.

Related

Why Doesn't #DocumentID Allow For Updates On List Row When Used As ID Parameter?

I am working on a SwiftUI Project and using Firestore as a backend. I am using #DocumentID to as the id property for my objects. I noticed that when I use the id to identify the item in a List/ForEach as part of a view, the row does not update if I update the object in Firebase. If change the id property to a String and remove the #DocumentID property wrapper then updates to the object in Firestore update the row.
The only thing I can think of is that when you use #DocumentID the property becomes the name of the document and the property is not stored in the document. When using a String the property is stored in the document. Below I have the code that uses #DocumentID and the String.
Listing.swift - Model - Using #DocumentID - Rows Don't Update When The Database Updates
struct Listing: Codable, Identifiable {
#DocumentID var id: String
var description: String
}
Listing.swift - Model - Using String - Rows Update When The Database Updates
struct Listing: Codable, Identifiable {
var id: String
var description: String
}
ListingRowViewModel.swift
This file assigns the id to an id variable on the ListingRowViewModel. The ListingRowViewModels used in the ForEach loop for the list.
class ListingRowViewModel: ObservableObject {
var id: String = ""
#Published var listing: Listing
private var cancellables = Set<AnyCancellable>()
init(listing: Listing) {
self.listing = listing
$listing
.receive(on: RunLoop.main)
.compactMap { listing in
listing.id
}
.assign(to: \.id, on: self)
.store(in: &cancellables)
}
}
List & ForEach Used in View
List {
ForEach(self.marketplaceViewModel.listingRowViewModels, id: \.id) { listingRowViewModel in
NavigationLink(destination: ListingDetailView(listingDetailViewModel: ListingDetailViewModel(listing: listingRowViewModel.listing))) {
ListingRowView(listing: listingRowViewModel.listing)
}
}
}
Is there a way to use the #DocumentID property wrapper and allow for updates to the row data when the object is updated in Firestore?

adobe flex create child object given a parent object

i have the following 2 classes
class A {
var one:int;
}
class B extends A {
var two:int;
}
I now have an object of the class A but need to create and object of class B and set the additional parameters.
Does Flex allow child object creation given a parent object ?
note: there are lot of parameters in A so i don't want to copy individually each one.
Flex does allow multiple constructors, but it would take the form of copying each parameter in the constructor.
public class B extends A {
var two:int;
public function B(instanceA:A) {
this.one = instanceA.one;
//etc
}
}
Another approach could be to use ObjectUtil:
public class B extends A {
var two:int;
public function B(instanceA:A) {
var data = ObjectUtil.getClassInfo(instanceA);
for each (var prop in data.properties) {
this[prop] = instanceA[prop];
}
}
}
n.b. Check the excludes and options arguments of getClassInfo -- if there are properties that would cause problems to be processed in this way, you'll want to make sure to exclude them.

Tornadofx Javafx - How to reload a view / component

So its a basic question.
What I am trying to achieve is refreshing views from another views.
Lets say I have a view EmployeeTableView which shows a tabular representation of employees by doing a REST API call.
In another view, I have a the filter EmployeeFilterView wherein I have gender, salary range, employee type, etc.
I also have a userContext object in which I store the user preferences. So by default lets say I have stored the value of gender filter to be Male, salary range to be ALL, etc. This object is send as a parameter to the EmployeeTableView.
When the EmployeeTableView is loaded I do a restAPI call with the userContext values to get the employee details. So that works fine. Now I change the gender filter to Female and assign this value in my userContext.
Now if I could just reload the EmployeeTableView with the userContext object, the restapi call would get the updated values.
But how can I do that ?
Also suggest a better approach if you have.
The EventBus is one valid solution to this. Another would be to use a ViewModel or Controller as the UserContext object and let that include the actual observable list of employees and then bind that list to the TableView in EmployeeTableView. Whenever the list in the context is updated, the TableView will update as well.
The filter view would call a function in the UserContext to perform the actual REST call and update the list of employees based on that.
You could create a separate EmployeeQuery object that can be injected into both the EmployeeFilterView and the UserContext so it can extract the selected filter values to perform the query. This query object contains a list of all the search parameters you want to pass to the server.
You could also consider creating a separate scope to keep these components separated if that makes sense to your architecture.
Exactly how you define these components are mostly a matter of taste, here is one suggestion. I used the RangeSlider from ControlsFX for the mock search UI.
To make it easier to imagine how this ties together, here is a screenshot:
(All names and salaries are fiction :)
/**
* The employee domain model, implementing JsonModel so it can be fetched
* via the REST API
*/
class Employee : JsonModel {
val nameProperty = SimpleStringProperty()
var name by nameProperty
val salaryProperty = SimpleIntegerProperty()
var salary by salaryProperty
val genderProperty = SimpleObjectProperty<Gender>()
var gender by genderProperty
override fun updateModel(json: JsonObject) {
with (json) {
name = getString("name")
salary = getInt("salary")
gender = Gender.valueOf(getString("gender"))
}
}
}
enum class Gender { Male, Female }
/**
* Container for the list of employees as well as a search function called by the filter
* view whenever it should update the employee list.
*/
class EmployeeContext : Controller() {
val api: Rest by inject()
val query: EmployeeQuery by inject()
val employees = SimpleListProperty<Employee>()
fun search() {
runAsync {
FXCollections.observableArrayList(Employee().apply {
name = "Edvin Syse"
gender = Gender.Male
salary = 200_000
})
//api.post("employees/query", query).list().toModel<Employee>()
} ui {
employees.value = it
}
}
}
/**
* Query object used to define the query sent to the server
*/
class EmployeeQuery : ViewModel(), JsonModel {
val genderProperty = SimpleObjectProperty<Gender>(Gender.Female)
var gender by genderProperty
val salaryMinProperty = SimpleIntegerProperty(50_000)
var salaryMin by salaryMinProperty
val salaryMaxProperty = SimpleIntegerProperty(250_000)
var salaryMax by salaryMaxProperty
val salaryDescription = stringBinding(salaryMinProperty, salaryMaxProperty) {
"$$salaryMin - $$salaryMax"
}
override fun toJSON(json: JsonBuilder) {
with(json) {
add("gender", gender.toString())
add("salaryMin", salaryMin)
add("salaryMax", salaryMax)
}
}
}
/**
* The search/filter UI
*/
class EmployeeFilterView : View() {
val query: EmployeeQuery by inject()
val context: EmployeeContext by inject()
override val root = form {
fieldset("Employee Filter") {
field("Gender") {
combobox(query.genderProperty, Gender.values().toList())
}
field("Salary Range") {
vbox {
alignment = Pos.CENTER
add(RangeSlider().apply {
max = 500_000.0
lowValueProperty().bindBidirectional(query.salaryMinProperty)
highValueProperty().bindBidirectional(query.salaryMaxProperty)
})
label(query.salaryDescription)
}
}
button("Search").action {
context.search()
}
}
}
}
/**
* The UI that shows the search results
*/
class EmployeeTableView : View() {
val context: EmployeeContext by inject()
override val root = borderpane {
center {
tableview(context.employees) {
column("Name", Employee::nameProperty)
column("Gender", Employee::genderProperty)
column("Salary", Employee::salaryProperty)
}
}
}
}
/**
* A sample view that ties the filter UI and result UI together
*/
class MainView : View("Employee App") {
override val root = hbox {
add(EmployeeFilterView::class)
add(EmployeeTableView::class)
}
}
I ended up using Tornadofx -> EventBus
Basically, when I change any of the filters, I fire an even which rebuilds the Node with the updated values.
Not sure whether the approach is right, that's why still keeping it open for discussion.

Creating a previously null parent, with child object with Newtonsoft

Using NewtonsoftJson, and without knowing the entire 'model', how do you create a new parent object and set a child value, when the parent doesn't already exist?
Existing parent
//The parent already exists, but the child does not.
jobj["Parent1"]["Child"] = true;
File.WriteAllText(mypath, JsonConvert.SerializeObject(joj, Formatting.Indented));
//output
//Successfully creates new child
New parent
//The parent does not exist, nor the child.
//Throws null reference exception as jobj["Parent3"] doesn't exist
jobj["Parent3"]["Child"] = true;
File.WriteAllText(mypath, JsonConvert.SerializeObject(joj, Formatting.Indented));
JSON
{
"parent1": {
"child": true
},
"parent2": {
"child": true
},
Well, you need to check whether the parent exists, and create it if it doesn't.
JToken parent = jobj["Parent3"];
if (parent == null)
{
// parent object doesn't exist so create it
parent = new JObject();
jobj["Parent3"] = parent;
}
parent["Child"] = true;
To add a new JProperty you need to add use JToken.FromObject within the JProperty constructor.
eg
jobj.Add(new JProperty("parent3", JToken.FromObject(new Parent { Child = true})));

Get all descendants types of base class

I have a base class called BaseEvent and several descendants classes:
public class BaseEvent {
// the some properties
// ...
}
[MapInheritance(MapInheritanceType.ParentTable)]
public class Film : BaseEvent {
// the some properties
// ...
}
[MapInheritance(MapInheritanceType.ParentTable)]
public class Concert : BaseEvent {
// the some properties
// ...
}
I have a code which create the BaseEvent instance at runtime:
BaseEvent event = new BaseEvent();
// assign values for a properties
// ...
baseEvent.XPObjectType = Database.XPObjectTypes.SingleOrDefault(
t => t.TypeName == "MyApp.Module.BO.Events.BaseEvent");
Now, this event will be shows in BaseEvent list view.
I want to do the following: when a user click Edit button then show in list view lookup field with all descendants types. And when user saves record change ObjectType to selected value.
How can I do this?
Thanks.
PS. this is asp.net app.
I'm not sure that your approach is correct for what you are trying to achieve. First, I'll answer the question you have asked, and afterwards I'll try to explain how the XAF already provides the functionality you are trying to achieve, namely how to choose which subclass of record to create from the user interface.
In order to create a property which allows the user to choose a Type within the application, you can declare a TypeConverter:
public class EventClassInfoTypeConverter : LocalizedClassInfoTypeConverter
{
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
List<Type> values = new List<Type>();
foreach (ITypeInfo info in XafTypesInfo.Instance.PersistentTypes)
{
if ((info.IsVisible && info.IsPersistent) && (info.Type != null))
{
// select BaseEvent subclasses
if (info.Type.IsSubclassOf(typeof(BaseEvent)))
values.Add(info.Type);
}
}
values.Sort(this);
values.Insert(0, null);
return new TypeConverter.StandardValuesCollection(values);
}
}
And then your base event class would look like:
public class BaseEvent: XPObject
{
public BaseEvent(Session session)
: base(session)
{ }
private Type _EventType;
[TypeConverter(typeof(EventClassInfoTypeConverter))]
public Type EventType
{
get
{
return _EventType;
}
set
{
SetPropertyValue("EventType", ref _EventType, value);
}
}
}
However, I suspect this is not the functionality you require. Modifying the value of the property will NOT change the base type of the record. That is, you will end up with a record of type BaseEvent which has a property Type equal to 'Concert' or 'Film'.
XAF already provides a mechanism for selecting the type of record to create. In your scenario, you will find that the New button is a dropdown with your different subclasses as options:
Therefore you do not need to create a 'type' property within your object. If you need a column to show the type of event in the list view, you can declare a property as follows
[PersistentAlias("XPObjectType.Name")]
public string EventType
{
get
{
return base.ClassInfo.ClassType.Name;
}
}

Resources