Multi Dimensional Byte Array In Kotlin - multidimensional-array

I have the following function in c#
public static byte[][] TagSplits(byte[][] splitArray, byte[] original, byte[] guid)
{
byte[] temp;
for (var a = 0; a < splitArray.Length; a++)
{
}
}
I am trying to convert the following code to Kotlin code, I have ended up with the following code:
companion object
{
fun TagSplits(splitArray: ByteArray, original: ByteArray, guid: ByteArray): ByteArray
{
var temp: ByteArray
for(a in 0..splitArray.size)
{
}
}
}
How would I be able to declare a multidimensional byte array in Kotlin as in the c# code base? For the input parameters for the

Arrays do not have special syntax in Kotlin. There are two ways how to work with arrays:
Using the specialized types ByteArray, IntArray etc. These correspond to Java byte[], int[] arrays.
Using the generic type Array<T>. This corresponds to a Java array of references T[].
You can achieve the nesting using Array<ByteArray>, but probably there is a better way of achieving what exactly you need. Alternatives are List<ByteArray> or a more high-level OOP representation of the byte patterns.

ByteArray is an object (reference type), so you can create an array of it: Array<ByteArray>.

Related

Flutter ObjectBox: Is List<String> supported for property converters?

The Flutter ObjectBox docs state:
ObjectBox (Java, Dart) has built-in support for String lists. ObjectBox for Java also has built-in support for String arrays.
However, using a List<String> in a converter triggers the following warning:
[WARNING] objectbox_generator:resolver on lib/model/diary_day.dart:
Skipping property 'doneExercises': type 'List' not supported, consider creating a relation for #Entity types (https://docs.objectbox.io/relations), or replace with getter/setter converting to a supported type (https://docs.objectbox.io/advanced/custom-types).
So the question now is, is this a bug, or am I misunderstanding the docs?
Just for reference, the converter looks like:
late List<DiaryExercise> doneExercises = [];
List<String> get dbDoneExercises {
List<String> retVal = [];
for (DiaryExercise thisExercise in doneExercises) {
retVal.add(jsonEncode(thisExercise.toJson()));
}
return retVal;
}
set dbDoneExercises(List<String> jsonStrings) {
for (String thisJson in jsonStrings)
DiaryExercise exercise = DiaryExercise.fromJson(jsonDecode(thisJson));
doneExercises.add(exercise);
}
}
Turned out, it's just a warning. A look into objectbox-model.json revealed the List<String> converter is used.

AutoFixture - customization using ISpecimenBuilder derived class - issues with casting request to PropertyInfo

I am using AutoFixture 4.17 in .NET 6
I am trying to create my own customization generator for DateTime
I read several tutorials and my code base on them.
It was said, that request parameter to Create method should be of type PropertyInfo so we can further analyze it. However, it is of type SeededRequest having inside Request property of anonymous type.
What is the best way now to analyze this Request field? Can I cast it somehow? To what type?
UPDATE:
I found a solution by casting it to dynamic type:
dynamic dyn = request as dynamic;
dynamic req = dyn.Request as dynamic;
if(req.Name != "DateTime")
but I'm not sure if this is the best approach.
You might want to learn more about reflection and pattern matching in .NET.
AutoFixture uses reflection a lot to be able to generate test data.
Here's a sample that might help you get started.
[Fact]
public void Foo()
{
var fixture = new Fixture();
fixture.Customizations.Add(new MyDateTimeGenerator());
var time = fixture.Create<DateTime>();
Assert.Equal(new DateTime(2011, 12, 13), time);
}
public class MyDateTimeGenerator : ISpecimenBuilder
{
public object Create(object request, ISpecimenContext context)
{
var isDateTimeRequest = request is Type type
&& type == typeof(DateTime);
if (!isDateTimeRequest)
return new NoSpecimen();
return new DateTime(2011, 12, 13);
}
}

How to make JsonConvert.DeserializeObject<T>() parse array when I won't always be passing array to it?

I have a method in my ASP.Net app that looks like this:
Method1<T>(String inputString)
{
return JsonConvert.DeserializeObject<T>(inputString);
}
I pass stringified objects to Method1, and one of them is a stringified version of this object:
obj1: {
a: ...
b: [...]
}
ie. obj1 is an object that has an array as a property. Now, as is, JsonConvert.DeserializeObject<T>(inputString) won't parse the array part of this object. I learned from this post that I could make this work if type1 were the type of obj1 and I did JsonConvert.DeserializeObject<type1>(inputString). The problem is that I'll be passing stringified versions of a variety of different types of objects to Method1, so I don't know how else to do it than with <T>.
Does anyone know how I can approach this?
Newtonsoft.Json.Linq api is great for this kind of scenario. You can parse your json into an abstract JToken object, and look at the token type to determine how to extract your array.
public MyType[] GetArrayFromJson(string json)
{
var token = JToken.Parse(json);
if (token.Type == JTokenType.Array)
{
return token.ToObject<MyType[]>();
}
else if(token.Type == JTokenType.Object)
{
return token["arrayPropName"].ToObject<MyType[]>();
}
}

ASP.NET GET parameters with square brackets

I don't know what they are called, that's why I can't find a solution.
I have a querystring looks like an array
params[name]=john&params[age]=25&params[country]=ru
Is there a way to parse these parameters as string[] or Dictionary<string, string>?
UPD:
On php this type of query string is being parsed automaticaly using
$params = $_GET['params'];
$params['name']
But I can't find an equivalent on C#
If you're using the ASP.NET MVC Framework, the default parameter binder that converts query string values into typed parameters can deal with it automatically in the following way:
One-Dimensional Arrays
// GetData?filters[0]=v1&filters[1]=v2&filters[3]=v3
public ActionResult GetData(IEnumerable<string> filters)
{
// todo
}
Two-Dimensional Arrays
// GetData?filters[0][field]=name&filters[0][type]=1&filters[0][value]=val
public ActionResult GetData(IEnumerable<Dictionary<string,string>> filters)
{
// todo
}
... and so on.
For more info and examples, check out this blog post that I wrote on this topic.

Use case example for copyToRealm() in Realm

Could someone give a simple use case example why someone would use copyToRealm() instead of createObject() ?
It is not clear to me why and when would anyone use copyToRealm() if there is createObject().
In the example here they seem pretty much the same https://realm.io/docs/java/latest/ .
copyToRealm() takes an unmanaged object and connects it to a Realm, while createObject() creates an object directly in a Realm.
For example it is very useful when you copy objects generated by GSON - returned from your Rest API into Realm.
realm.createObject() also returns a RealmProxy instance and is manipulated directly and therefore creates N objects to store N objects, however you can use the following pattern to use only 1 instance of object to store N objects:
RealmUtils.executeInTransaction(realm -> {
Cat defaultCat = new Cat(); // unmanaged realm object
for(CatBO catBO : catsBO.getCats()) {
defaultCat.setId(catBO.getId());
defaultCat.setSourceUrl(catBO.getSourceUrl());
defaultCat.setUrl(catBO.getUrl());
realm.insertOrUpdate(defaultCat);
}
});
But to actually answer your question, copyToRealmOrUpdate() makes sense if you want to persist elements, put them in a RealmList<T> and set that RealmList of newly managed objects in another RealmObject. It happens mostly if your RealmObject classes and the downloaded parsed objects match.
#JsonObject
public class Cat extends RealmObject {
#PrimaryKey
#JsonField(name="id")
String id;
#JsonField(name="source_url")
String sourceUrl;
#JsonField(name="url")
String url;
// getters, setters;
}
final List<Cat> cats = //get from LoganSquare;
realm.executeTransaction(new Realm.Transaction() {
#Override
public void execute(Realm realm) {
Person person = realm.where(Person.class).equalTo("id", id).findFirst();
RealmList<Cat> realmCats = new RealmList<>();
for(Cat cat : realm.copyToRealmOrUpdate(cats)) {
realmCats.add(cat);
}
person.setCats(realmCats);
}
});

Resources