jsonix icon indicating copy to clipboard operation
jsonix copied to clipboard

Typescript definition

Open duschata opened this issue 9 years ago • 13 comments
trafficstars

I think there is no d.ts for Jsonix (I've not found one in this project or at DefinitelyTyped). I've initiated one. OK for you to contribute it via PR?

Greetings, Tom

duschata avatar Apr 14 '16 12:04 duschata

Would be great! I'm generally thinking about moving to TS completely.

highsource avatar Apr 14 '16 12:04 highsource

in progress: https://github.com/duschata/jsonix/tree/issue_%23139

duschata avatar Apr 16 '16 08:04 duschata

Hi, thanks for the TS definitions.

What is the point of the java files in the typescript dir, to make the SVG classes diagram ?

I also started doing typescript definitons for jsonix, but didn't go far enough. I wanted to make the unmarshalled object strongly typed, with pseudo tagged union with the help of User Defined Type Guards and code generation from the output of jsonix compiler json. I saw there is indications in the code about it too. What are you thought about it ?

amarant avatar Apr 29 '16 13:04 amarant

Hello Arnaud, I'm Tom and the initiator of the jsonix typescript definitions. You will find now an english README in the next PR of my branch and I hope this will answer your first question. look at https://github.com/duschata/jsonix/tree/issue_%23139/typescript in case the branch is not merged yet. If I understand your second question, you are looking for a type guard for your data entities. TS doesn't know anything about the instances of the types, so it doesn't make so much sense to generate TS.d.ts definitions by the jsonix-schema-compiler (unless there is a reflection API for the t.ds files, which I don't know). But you can easily create your own type guard by using the

getTypeInfoByName(name:string):TypeInfo interface

(have a look at the d.ts, you will see, what you get back)

than try something like this:

class TypeGuard {

 public isTypeOf(typeInfo: TypeInfo, myObject: Object) {

     //safe because a typeInfo which has properties is a ClassInfo
     for (let propertyIndex in (<ClassInfo>typeInfo).properties) {

         let property: ElementPropertyInfo = 

<ElementPropertyInfo>(<ClassInfo>typeInfo).properties[propertyIndex]; if (typeof myObject[property.name] === 'undefined') { return false; } else { // get typeInfo = property.typeInfo, shift object pointer and go recursive }

         return true;
     }
 }

}

in your app:

//get the jsonix context let typeGuard: TypeGuard = new TypeGuard(); let result: boolean = typeGuard.isTypeOf(jsonixContext.getTypeInfoByName("myNs.MyType"), {firstField:'', secondFild:''})

Tom

Am 29.04.2016 um 15:16 schrieb Arnaud Marant:

Hi, thanks for the TS definitions.

What is the point of the java files in the typescript dir https://github.com/highsource/jsonix/tree/master/typescript/src/main/java/org/hisrc/jsonix, to make the SVG classes diagram ?

I also started doing typescript definitons for jsonix, but didn't go far enough. I wanted to make the unmarshalled object strongly typed, with pseudo tagged union with the help of User Defined Type Guards https://basarat.gitbooks.io/typescript/content/docs/types/typeGuard.html and code generation from the output of jsonix compiler json. I saw there is indications in the code about it too. What are you thought about it ?

— You are receiving this because you authored the thread. Reply to this email directly or view it on GitHub https://github.com/highsource/jsonix/issues/139#issuecomment-215708005

Diese Mail ist mit meinem x.509 Zertifikat signiert. Dadurch ist es möglich verschlüsselte Mails an mich zu senden. Eine Anleitung dazu findet sich unter:

http://www.thunderbird-mail.de/wiki/Mailverschl%C3%BCsselung_mit_S/MIME

Falls das Zertifikat z.B. durch eine Mailingliste entfernt wurde, kann man es hier herunterladen:

https://toms-toy.de/[email protected]

Wer PGP benutzt findet meinen Public-Key hier:

https://toms-toy.de/[email protected]_pub.asc

GIT d-(---)? s():()> a+ C$>++ UL$>+ P? L>++ E W>+++ N>++$ o K--? w>--- O M>-- V PS>+ PE Y PGP>++$ t 5 X R tv b>+++ DI D G e> h>++ !r y>+

duschata avatar Apr 29 '16 18:04 duschata

I have issues importing the Jsonix module like:

import {Jsonix} from "../../node_modules/jsonix/jsonix"; results in: Error TS2307: Cannot find module '../../node_modules/jsonix/jsonix'. even though I imported the type definition from

"globalDependencies": {
    "jsonix": "https://raw.githubusercontent.com/duschata/jsonix/issue_%23139/typescript/src/main/typescript/Jsonix.d.ts",
    "es6-shim": "registry:dt/es6-shim#0.31.2+20160317120654",
....
}

But during run-time it works just fine. This is only a compile-time issue.

geoHeil avatar Jun 14 '16 18:06 geoHeil

can you give a little bit more of code / or plunker, please?

duschata avatar Jun 15 '16 10:06 duschata

https://github.com/geoHeil/ionic2WebpackTypescriptNonNativeModule please run ionic serve

you should see the error from above in the console.

geoHeil avatar Jun 15 '16 11:06 geoHeil

May be you mean this? app/pages/home-page/home-page.ts(4,22): error TS2307: Cannot find module '../../node_modules/jsonix/jsonix.js'.

you can only import *.ts files. But this is a reference to a *.js file. (the import statement is needed by the tsc to compiler, but this file is already js) So this line is useless / wrong. You can delete it. For the compile time you need only the d.ts reference. For the runtime you have to include the .js sources (eg in the index.html).

duschata avatar Jun 17 '16 12:06 duschata

thanks, that solves this issue.

geoHeil avatar Jun 17 '16 12:06 geoHeil

Hi duschata, Highsource,

Thanks for your typescript definition though it doesn't seem to be complete. It says on line 276:

//TODO: confirm this syntax
propertyInfoCreators: {
    aa: { aa };
    anyAttribute: { aa },
    ae: { ae };
    ...

And you are correct that this doesn't seem to be the correct syntax. I'm sorry I don't know either and am wondering where you got up to with this typing.

I changed the types to any, which seemed to work (it compiled) though I haven't been successful at importing it into my typescript. Other node_modules such as yargs with separate typings works so I don't know why this doesn't. Do you have any ides? This is what I did:

// add this to `typings.json`
"jsonix": "https://raw.githubusercontent.com/duschata/jsonix/master/typescript/src/main/typescript/Jsonix.d.ts",

// run these
npm install jsonix --save
typings install

I confirmed there is a typings/global/jsonix/index.d.ts and typings.json. And that there is a node_modules/jsonix.

// And add this to the `.ts` file - IntelliJ's intellisense suggested the 'jsonix' is correct but didn't have any suggestions for where I entered '{ Jsonix }'.

import { Jsonix } from 'jsonix';

It errors with:

src/client/app/shared/jsonix/jsonix.service.ts(5,24): error TS2307: Cannot find module 'jsonix'.

I thought that what I've done is sufficient but obviously not. Do you have any idea what is missing?

oehm-smith avatar Sep 08 '16 06:09 oehm-smith

Is there any progress on this? Would really like to have types for jsonix...

ghost avatar Jul 09 '19 11:07 ghost

I've not worked for years on the TS-Definitions. This is the last state and it was sufficient for my needs

https://github.com/highsource/jsonix/blob/master/typescript/src/main/typescript/Jsonix.d.ts

duschata avatar Jul 09 '19 11:07 duschata

@highsource, it's been several years since this ticket was opened, During that time TypeScript has exploded in popularity - still planning to convert JSONIX to TS?

Unless I'm mistaken, JSONIX is still the most awesome library for JS/XML serialization. Would be even more awesome if natively TS.

ShawnTalbert avatar Oct 16 '20 14:10 ShawnTalbert